Merge branch 'master' of ssh://git.samba.org/data/git/samba
[kamenim/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         }
62
63         # we don't support multi-dimentional arrays yet
64         if ($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         }
70
71         if ($l->{TYPE} eq "ARRAY") {
72                 my $size = ParseExpr($l->{SIZE_IS}, $env, $e);
73                 pidl "$name = talloc_zero_array($mem_ctx, " . DeclLevel($e, 1) . ", $size);";
74         } else {
75                 pidl "$name = talloc_zero($mem_ctx, " . DeclLevel($e, 1) . ");";
76         }
77
78         pidl "if ($name == NULL) {";
79         $fail->();
80         pidl "}";
81         pidl "";
82 }
83
84 sub CallWithStruct($$$$)
85 {
86         my ($pipes_struct, $mem_ctx, $fn, $fail) = @_;
87         my $env = GenerateFunctionOutEnv($fn);
88         my $hasout = 0;
89         foreach (@{$fn->{ELEMENTS}}) {
90                 if (grep(/out/, @{$_->{DIRECTION}})) { $hasout = 1; }
91         }
92
93         pidl "ZERO_STRUCT(r->out);" if ($hasout);
94
95         my $proto = "_$fn->{NAME}(pipes_struct *p, struct $fn->{NAME} *r";
96         my $ret = "_$fn->{NAME}($pipes_struct, r";
97         foreach (@{$fn->{ELEMENTS}}) {
98                 my @dir = @{$_->{DIRECTION}};
99                 if (grep(/in/, @dir) and grep(/out/, @dir)) {
100                         pidl "r->out.$_->{NAME} = r->in.$_->{NAME};";
101                 }
102         }
103
104         foreach (@{$fn->{ELEMENTS}}) {
105                 my @dir = @{$_->{DIRECTION}};
106                 if (grep(/in/, @dir) and grep(/out/, @dir)) {
107                         # noop
108                 } elsif (grep(/out/, @dir) and not
109                                  has_property($_, "represent_as")) {
110                         AllocOutVar($_, $mem_ctx, "r->out.$_->{NAME}", $env, $fail);
111                 }
112         }
113         $ret .= ")";
114         $proto .= ");";
115
116         if ($fn->{RETURN_TYPE}) {
117                 $ret = "r->out.result = $ret";
118                 $proto = "$fn->{RETURN_TYPE} $proto";
119         } else {
120                 $proto = "void $proto";
121         }
122
123         pidl_hdr "$proto";
124         pidl "$ret;";
125 }
126
127 sub ParseFunction($$)
128 {
129         my ($if,$fn) = @_;
130
131         my $op = "NDR_".uc($fn->{NAME});
132
133         pidl "static bool api_$fn->{NAME}(pipes_struct *p)";
134         pidl "{";
135         indent;
136         pidl "const struct ndr_interface_call *call;";
137         pidl "struct ndr_pull *pull;";
138         pidl "struct ndr_push *push;";
139         pidl "enum ndr_err_code ndr_err;";
140         pidl "DATA_BLOB blob;";
141         pidl "struct $fn->{NAME} *r;";
142         pidl "";
143         pidl "call = &ndr_table_$if->{NAME}.calls[$op];";
144         pidl "";
145         pidl "r = talloc(talloc_tos(), struct $fn->{NAME});";
146         pidl "if (r == NULL) {";
147         pidl "\treturn false;";
148         pidl "}";
149         pidl "";
150         pidl "if (!prs_data_blob(&p->in_data.data, &blob, r)) {";
151         pidl "\ttalloc_free(r);";
152         pidl "\treturn false;";
153         pidl "}";
154         pidl "";
155         pidl "pull = ndr_pull_init_blob(&blob, r, NULL);";
156         pidl "if (pull == NULL) {";
157         pidl "\ttalloc_free(r);";
158         pidl "\treturn false;";
159         pidl "}";
160         pidl "";
161         pidl "pull->flags |= LIBNDR_FLAG_REF_ALLOC;";
162         pidl "ndr_err = call->ndr_pull(pull, NDR_IN, r);";
163         pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {";
164         pidl "\ttalloc_free(r);";
165         pidl "\treturn false;";
166         pidl "}";
167         pidl "";
168         pidl "if (DEBUGLEVEL >= 10) {";
169         pidl "\tNDR_PRINT_IN_DEBUG($fn->{NAME}, r);";
170         pidl "}";
171         pidl "";
172
173         CallWithStruct("p", "r", $fn, 
174         sub { 
175                         pidl "\ttalloc_free(r);";
176                         pidl "\treturn false;";
177                 }
178         );
179
180         pidl "";
181         pidl "if (p->rng_fault_state) {";
182         pidl "\ttalloc_free(r);";
183         pidl "\t/* Return true here, srv_pipe_hnd.c will take care */";
184         pidl "\treturn true;";
185         pidl "}";
186         pidl "";
187         pidl "if (DEBUGLEVEL >= 10) {";
188         pidl "\tNDR_PRINT_OUT_DEBUG($fn->{NAME}, r);";
189         pidl "}";
190         pidl "";
191         pidl "push = ndr_push_init_ctx(r, NULL);";
192         pidl "if (push == NULL) {";
193         pidl "\ttalloc_free(r);";
194         pidl "\treturn false;";
195         pidl "}";
196         pidl "";
197         pidl "ndr_err = call->ndr_push(push, NDR_OUT, r);";
198         pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {";
199         pidl "\ttalloc_free(r);";
200         pidl "\treturn false;";
201         pidl "}";
202         pidl "";
203         pidl "blob = ndr_push_blob(push);";
204         pidl "if (!prs_copy_data_in(&p->out_data.rdata, (const char *)blob.data, (uint32_t)blob.length)) {";
205         pidl "\ttalloc_free(r);";
206         pidl "\treturn false;";
207         pidl "}";
208         pidl "";
209         pidl "talloc_free(r);";
210         pidl "";
211         pidl "return true;";
212         deindent;
213         pidl "}";
214         pidl "";
215 }
216
217 sub ParseDispatchFunction($)
218 {
219         my ($if) = @_;
220
221         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);";
222         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)";
223         pidl "{";
224         indent;
225         pidl "if (cli->pipes_struct == NULL) {";
226         pidl "\treturn NT_STATUS_INVALID_PARAMETER;";
227         pidl "}";
228         pidl "";
229         pidl "switch (opnum)";
230         pidl "{";
231         indent;
232         foreach my $fn (@{$if->{FUNCTIONS}}) {
233                 next if ($fn->{PROPERTIES}{noopnum});
234                 my $op = "NDR_".uc($fn->{NAME});
235                 pidl "case $op: {";
236                 indent;
237                 pidl "struct $fn->{NAME} *r = _r;";
238                 CallWithStruct("cli->pipes_struct", "mem_ctx", $fn, 
239                         sub { pidl "return NT_STATUS_NO_MEMORY;"; });
240                 pidl "return NT_STATUS_OK;";
241                 deindent;
242                 pidl "}";
243                 pidl "";
244         }
245
246         pidl "default:";
247         pidl "\treturn NT_STATUS_NOT_IMPLEMENTED;";
248         deindent;
249         pidl "}";
250         deindent;
251         pidl "}";
252
253         pidl "";
254 }
255
256 sub ParseInterface($)
257 {
258         my $if = shift;
259
260         my $uif = uc($if->{NAME});
261
262         pidl_hdr "#ifndef __SRV_$uif\__";
263         pidl_hdr "#define __SRV_$uif\__";
264
265         foreach (@{$if->{FUNCTIONS}}) {
266                 next if ($_->{PROPERTIES}{noopnum});
267                 ParseFunction($if, $_);
268         }
269
270         pidl "";
271         pidl "/* Tables */";
272         pidl "static struct api_struct api_$if->{NAME}_cmds[] = ";
273         pidl "{";
274         indent;
275
276         foreach (@{$if->{FUNCTIONS}}) {
277                 next if ($_->{PROPERTIES}{noopnum});
278                 pidl "{\"" . uc($_->{NAME}) . "\", NDR_" . uc($_->{NAME}) . ", api_$_->{NAME}},";
279         }
280
281         deindent;
282         pidl "};";
283
284         pidl "";
285
286         pidl_hdr "void $if->{NAME}_get_pipe_fns(struct api_struct **fns, int *n_fns);";
287         pidl "void $if->{NAME}_get_pipe_fns(struct api_struct **fns, int *n_fns)";
288         pidl "{";
289         indent;
290         pidl "*fns = api_$if->{NAME}_cmds;";
291         pidl "*n_fns = sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct);";
292         deindent;
293         pidl "}";
294         pidl "";
295
296         ParseDispatchFunction($if);
297
298         pidl_hdr "NTSTATUS rpc_$if->{NAME}_init(void);";
299         pidl "NTSTATUS rpc_$if->{NAME}_init(void)";
300         pidl "{";
301         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));";
302         pidl "}";
303
304         pidl_hdr "#endif /* __SRV_$uif\__ */";
305 }
306
307 sub Parse($$$)
308 {
309         my($ndr,$header,$ndr_header) = @_;
310
311         $res = "";
312         $res_hdr = "";
313
314         pidl "/*";
315         pidl " * Unix SMB/CIFS implementation.";
316         pidl " * server auto-generated by pidl. DO NOT MODIFY!";
317         pidl " */";
318         pidl "";
319         pidl "#include \"includes.h\"";
320         pidl "#include \"$header\"";
321         pidl_hdr "#include \"$ndr_header\"";
322         pidl "";
323
324         foreach (@$ndr) {
325                 ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
326         }
327
328         return ($res, $res_hdr);
329 }
330
331 1;