Debian Codegraph @

pidl/lib/Parse/Pidl/Samba3/ServerNDR.pm

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