Merge branch 'master' of ssh://git.samba.org/data/git/samba
[samba.git] / pidl / lib / Parse / Pidl / Samba3 / ServerNDR.pm
1 ###################################################
2 # Samba3 server generator for IDL structures
3 # on top of Samba4 style NDR functions
4 # Copyright jelmer@samba.org 2005-2006
5 # released under the GNU GPL
6
7 package Parse::Pidl::Samba3::ServerNDR;
8
9 use Exporter;
10 @ISA = qw(Exporter);
11 @EXPORT_OK = qw(DeclLevel);
12
13 use strict;
14 use Parse::Pidl qw(warning fatal);
15 use Parse::Pidl::Typelist qw(mapTypeName scalar_is_reference);
16 use Parse::Pidl::Util qw(ParseExpr has_property is_constant);
17 use Parse::Pidl::NDR qw(GetNextLevel);
18 use Parse::Pidl::Samba4 qw(ElementStars DeclLong);
19 use Parse::Pidl::Samba4::Header qw(GenerateFunctionOutEnv);
20
21 use vars qw($VERSION);
22 $VERSION = '0.01';
23
24 my $res;
25 my $res_hdr;
26 my $tabs = "";
27 sub indent() { $tabs.="\t"; }
28 sub deindent() { $tabs = substr($tabs, 1); }
29 sub pidl($) { my ($txt) = @_; $res .= $txt?$tabs.(shift)."\n":"\n"; }
30 sub pidl_hdr($) { $res_hdr .= (shift)."\n"; }
31 sub fn_declare($) { my ($n) = @_; pidl $n; pidl_hdr "$n;"; }
32
33 sub DeclLevel($$)
34 {
35         my ($e, $l) = @_;
36         my $res = "";
37
38         if (has_property($e, "charset")) {
39                 $res .= "const char";
40         } else {
41                 $res .= mapTypeName($e->{TYPE});
42         }
43
44         my $stars = ElementStars($e, $l);
45
46         $res .= " ".$stars unless ($stars eq "");
47
48         return $res;
49 }
50
51 sub AllocOutVar($$$$$)
52 {
53         my ($e, $mem_ctx, $name, $env, $fail) = @_;
54
55         my $l = $e->{LEVELS}[0];
56
57         # we skip pointer to arrays
58         if ($l->{TYPE} eq "POINTER") {
59                 my $nl = GetNextLevel($e, $l);
60                 $l = $nl if ($nl->{TYPE} eq "ARRAY");
61         } elsif
62
63         # we don't support multi-dimentional arrays yet
64         ($l->{TYPE} eq "ARRAY") {
65                 my $nl = GetNextLevel($e, $l);
66                 if ($nl->{TYPE} eq "ARRAY") {
67                         fatal($e->{ORIGINAL},"multi-dimentional [out] arrays are not supported!");
68                 }
69         } else {
70                 # neither pointer nor array, no need to alloc something.
71                 return;
72         }
73
74         if ($l->{TYPE} eq "ARRAY") {
75                 my $size = ParseExpr($l->{SIZE_IS}, $env, $e);
76                 pidl "$name = talloc_zero_array($mem_ctx, " . DeclLevel($e, 1) . ", $size);";
77         } else {
78                 pidl "$name = talloc_zero($mem_ctx, " . DeclLevel($e, 1) . ");";
79         }
80
81         pidl "if ($name == NULL) {";
82         $fail->();
83         pidl "}";
84         pidl "";
85 }
86
87 sub CallWithStruct($$$$)
88 {
89         my ($pipes_struct, $mem_ctx, $fn, $fail) = @_;
90         my $env = GenerateFunctionOutEnv($fn);
91         my $hasout = 0;
92         foreach (@{$fn->{ELEMENTS}}) {
93                 if (grep(/out/, @{$_->{DIRECTION}})) { $hasout = 1; }
94         }
95
96         pidl "ZERO_STRUCT(r->out);" if ($hasout);
97
98         my $proto = "_$fn->{NAME}(pipes_struct *p, struct $fn->{NAME} *r";
99         my $ret = "_$fn->{NAME}($pipes_struct, r";
100         foreach (@{$fn->{ELEMENTS}}) {
101                 my @dir = @{$_->{DIRECTION}};
102                 if (grep(/in/, @dir) and grep(/out/, @dir)) {
103                         pidl "r->out.$_->{NAME} = r->in.$_->{NAME};";
104                 }
105         }
106
107         foreach (@{$fn->{ELEMENTS}}) {
108                 my @dir = @{$_->{DIRECTION}};
109                 if (grep(/in/, @dir) and grep(/out/, @dir)) {
110                         # noop
111                 } elsif (grep(/out/, @dir) and not
112                                  has_property($_, "represent_as")) {
113                         AllocOutVar($_, $mem_ctx, "r->out.$_->{NAME}", $env, $fail);
114                 }
115         }
116         $ret .= ")";
117         $proto .= ");";
118
119         if ($fn->{RETURN_TYPE}) {
120                 $ret = "r->out.result = $ret";
121                 $proto = "$fn->{RETURN_TYPE} $proto";
122         } else {
123                 $proto = "void $proto";
124         }
125
126         pidl_hdr "$proto";
127         pidl "$ret;";
128 }
129
130 sub ParseFunction($$)
131 {
132         my ($if,$fn) = @_;
133
134         my $op = "NDR_".uc($fn->{NAME});
135
136         pidl "static bool api_$fn->{NAME}(pipes_struct *p)";
137         pidl "{";
138         indent;
139         pidl "const struct ndr_interface_call *call;";
140         pidl "struct ndr_pull *pull;";
141         pidl "struct ndr_push *push;";
142         pidl "enum ndr_err_code ndr_err;";
143         pidl "DATA_BLOB blob;";
144         pidl "struct $fn->{NAME} *r;";
145         pidl "";
146         pidl "call = &ndr_table_$if->{NAME}.calls[$op];";
147         pidl "";
148         pidl "r = talloc(talloc_tos(), struct $fn->{NAME});";
149         pidl "if (r == NULL) {";
150         pidl "\treturn false;";
151         pidl "}";
152         pidl "";
153         pidl "if (!prs_data_blob(&p->in_data.data, &blob, r)) {";
154         pidl "\ttalloc_free(r);";
155         pidl "\treturn false;";
156         pidl "}";
157         pidl "";
158         pidl "pull = ndr_pull_init_blob(&blob, r, NULL);";
159         pidl "if (pull == NULL) {";
160         pidl "\ttalloc_free(r);";
161         pidl "\treturn false;";
162         pidl "}";
163         pidl "";
164         pidl "pull->flags |= LIBNDR_FLAG_REF_ALLOC;";
165         pidl "ndr_err = call->ndr_pull(pull, NDR_IN, r);";
166         pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {";
167         pidl "\ttalloc_free(r);";
168         pidl "\treturn false;";
169         pidl "}";
170         pidl "";
171         pidl "if (DEBUGLEVEL >= 10) {";
172         pidl "\tNDR_PRINT_IN_DEBUG($fn->{NAME}, r);";
173         pidl "}";
174         pidl "";
175
176         CallWithStruct("p", "r", $fn, 
177         sub { 
178                         pidl "\ttalloc_free(r);";
179                         pidl "\treturn false;";
180                 }
181         );
182
183         pidl "";
184         pidl "if (p->rng_fault_state) {";
185         pidl "\ttalloc_free(r);";
186         pidl "\t/* Return true here, srv_pipe_hnd.c will take care */";
187         pidl "\treturn true;";
188         pidl "}";
189         pidl "";
190         pidl "if (DEBUGLEVEL >= 10) {";
191         pidl "\tNDR_PRINT_OUT_DEBUG($fn->{NAME}, r);";
192         pidl "}";
193         pidl "";
194         pidl "push = ndr_push_init_ctx(r, NULL);";
195         pidl "if (push == NULL) {";
196         pidl "\ttalloc_free(r);";
197         pidl "\treturn false;";
198         pidl "}";
199         pidl "";
200         pidl "ndr_err = call->ndr_push(push, NDR_OUT, r);";
201         pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {";
202         pidl "\ttalloc_free(r);";
203         pidl "\treturn false;";
204         pidl "}";
205         pidl "";
206         pidl "blob = ndr_push_blob(push);";
207         pidl "if (!prs_copy_data_in(&p->out_data.rdata, (const char *)blob.data, (uint32_t)blob.length)) {";
208         pidl "\ttalloc_free(r);";
209         pidl "\treturn false;";
210         pidl "}";
211         pidl "";
212         pidl "talloc_free(r);";
213         pidl "";
214         pidl "return true;";
215         deindent;
216         pidl "}";
217         pidl "";
218 }
219
220 sub ParseDispatchFunction($)
221 {
222         my ($if) = @_;
223
224         pidl_hdr "NTSTATUS rpc_$if->{NAME}_dispatch(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, const struct ndr_interface_table *table, uint32_t opnum, void *r);";
225         pidl "NTSTATUS rpc_$if->{NAME}_dispatch(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, const struct ndr_interface_table *table, uint32_t opnum, void *_r)";
226         pidl "{";
227         indent;
228         pidl "if (cli->pipes_struct == NULL) {";
229         pidl "\treturn NT_STATUS_INVALID_PARAMETER;";
230         pidl "}";
231         pidl "";
232         pidl "switch (opnum)";
233         pidl "{";
234         indent;
235         foreach my $fn (@{$if->{FUNCTIONS}}) {
236                 next if ($fn->{PROPERTIES}{noopnum});
237                 my $op = "NDR_".uc($fn->{NAME});
238                 pidl "case $op: {";
239                 indent;
240                 pidl "struct $fn->{NAME} *r = (struct $fn->{NAME} *)_r;";
241                 CallWithStruct("cli->pipes_struct", "mem_ctx", $fn, 
242                         sub { pidl "return NT_STATUS_NO_MEMORY;"; });
243                 pidl "return NT_STATUS_OK;";
244                 deindent;
245                 pidl "}";
246                 pidl "";
247         }
248
249         pidl "default:";
250         pidl "\treturn NT_STATUS_NOT_IMPLEMENTED;";
251         deindent;
252         pidl "}";
253         deindent;
254         pidl "}";
255
256         pidl "";
257 }
258
259 sub ParseInterface($)
260 {
261         my $if = shift;
262
263         my $uif = uc($if->{NAME});
264
265         pidl_hdr "#ifndef __SRV_$uif\__";
266         pidl_hdr "#define __SRV_$uif\__";
267
268         foreach (@{$if->{FUNCTIONS}}) {
269                 next if ($_->{PROPERTIES}{noopnum});
270                 ParseFunction($if, $_);
271         }
272
273         pidl "";
274         pidl "/* Tables */";
275         pidl "static struct api_struct api_$if->{NAME}_cmds[] = ";
276         pidl "{";
277         indent;
278
279         foreach (@{$if->{FUNCTIONS}}) {
280                 next if ($_->{PROPERTIES}{noopnum});
281                 pidl "{\"" . uc($_->{NAME}) . "\", NDR_" . uc($_->{NAME}) . ", api_$_->{NAME}},";
282         }
283
284         deindent;
285         pidl "};";
286
287         pidl "";
288
289         pidl_hdr "void $if->{NAME}_get_pipe_fns(struct api_struct **fns, int *n_fns);";
290         pidl "void $if->{NAME}_get_pipe_fns(struct api_struct **fns, int *n_fns)";
291         pidl "{";
292         indent;
293         pidl "*fns = api_$if->{NAME}_cmds;";
294         pidl "*n_fns = sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct);";
295         deindent;
296         pidl "}";
297         pidl "";
298
299         ParseDispatchFunction($if);
300
301         pidl_hdr "NTSTATUS rpc_$if->{NAME}_init(void);";
302         pidl "NTSTATUS rpc_$if->{NAME}_init(void)";
303         pidl "{";
304         pidl "\treturn rpc_srv_register(SMB_RPC_INTERFACE_VERSION, \"$if->{NAME}\", \"$if->{NAME}\", \&ndr_table_$if->{NAME}, api_$if->{NAME}_cmds, sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct));";
305         pidl "}";
306
307         pidl_hdr "#endif /* __SRV_$uif\__ */";
308 }
309
310 sub Parse($$$)
311 {
312         my($ndr,$header,$ndr_header) = @_;
313
314         $res = "";
315         $res_hdr = "";
316
317         pidl "/*";
318         pidl " * Unix SMB/CIFS implementation.";
319         pidl " * server auto-generated by pidl. DO NOT MODIFY!";
320         pidl " */";
321         pidl "";
322         pidl "#include \"includes.h\"";
323         pidl "#include \"$header\"";
324         pidl_hdr "#include \"$ndr_header\"";
325         pidl "";
326
327         foreach (@$ndr) {
328                 ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
329         }
330
331         return ($res, $res_hdr);
332 }
333
334 1;