1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
###################################################
# client calls generator
# Copyright tridge@samba.org 2003
# Copyright jelmer@samba.org 2005-2006
# released under the GNU GPL
package Parse::Pidl::Samba4::NDR::Client;
use Parse::Pidl::Samba4 qw(choose_header is_intree);
use Parse::Pidl::Util qw(has_property);
use vars qw($VERSION);
$VERSION = '0.01';
use strict;
my($res,$res_hdr);
sub ParseFunctionOldSend($$$)
{
my ($interface, $fn, $name) = @_;
my $uname = uc $name;
if (has_property($fn, "todo")) {
return;
}
my $proto = "static struct rpc_request *dcerpc_$name\_send(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $name *r)";
$res .= "$proto\n{\n";
$res .= "
if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) {
NDR_PRINT_IN_DEBUG($name, r);
}
return dcerpc_ndr_request_send(p, NULL, &ndr_table_$interface->{NAME},
NDR_$uname, true, mem_ctx, r);
";
$res .= "}\n\n";
}
sub ParseFunctionOldRecv($$$)
{
my ($interface, $fn, $name) = @_;
my $uname = uc $name;
if (has_property($fn, "todo")) {
return;
}
my $proto = "static NTSTATUS dcerpc_$name\_recv(struct rpc_request *rreq)";
$res .= "$proto\n{\n";
$res .= "NTSTATUS status;
struct dcerpc_pipe *p = rreq->p;
struct $name *r = (struct $name *)rreq->ndr.struct_ptr;
status = dcerpc_ndr_request_recv(rreq);
if (NT_STATUS_IS_OK(status) && (p->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
NDR_PRINT_OUT_DEBUG($name, r);
}
return status;
";
$res .= "}\n\n";
}
sub ParseFunctionSync($$$)
{
my ($interface, $fn, $name) = @_;
my $uname = uc $name;
if (has_property($fn, "todo")) {
return;
}
my $proto = "NTSTATUS dcerpc_$name(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $name *r)";
$res_hdr .= "\n$proto;\n";
$res .= "$proto\n{\n";
$res .= "
NTSTATUS status;
if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) {
NDR_PRINT_IN_DEBUG($name, r);
}
status = dcerpc_ndr_request(p, NULL, &ndr_table_$interface->{NAME},
NDR_$uname, mem_ctx, r);
if (NT_STATUS_IS_OK(status) && (p->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
NDR_PRINT_OUT_DEBUG($name, r);
}
";
if (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "NTSTATUS") {
$res .= "\tif (NT_STATUS_IS_OK(status)) status = r->out.result;\n";
}
$res .=
"
return status;
";
$res .= "}\n\n";
}
sub ParseFunction_r_State($$$)
{
my ($interface, $fn, $name) = @_;
my $uname = uc $name;
if (has_property($fn, "todo")) {
return;
}
$res .= "struct dcerpc_$name\_r_state {\n";
$res .= "\tTALLOC_CTX *out_mem_ctx;\n";
$res .= "};\n";
$res .= "\n";
$res .= "static void dcerpc_$name\_r_done(struct rpc_request *subreq);\n";
$res .= "\n";
}
sub ParseFunction_r_Send($$$)
{
my ($interface, $fn, $name) = @_;
my $uname = uc $name;
if (has_property($fn, "todo")) {
return;
}
my $proto = "struct tevent_req *dcerpc_$name\_r_send(TALLOC_CTX *mem_ctx,\n";
$proto .= "\tstruct tevent_context *ev,\n",
$proto .= "\tstruct dcerpc_binding_handle *h,\n",
$proto .= "\tstruct $name *r)";
$res_hdr .= "\n$proto;\n";
$res .= "$proto\n{\n";
$res .= "\tstruct tevent_req *req;\n";
$res .= "\tstruct dcerpc_$name\_r_state *state;\n";
$res .= "\tstruct dcerpc_pipe *p =\n";
$res .= "\t\ttalloc_get_type_abort(h->private_data,\n";
$res .= "\t\tstruct dcerpc_pipe);\n";
$res .= "\tstruct rpc_request *subreq;\n";
$res .= "\n";
$res .= "\treq = tevent_req_create(mem_ctx, &state,\n";
$res .= "\t\t\t\tstruct dcerpc_$name\_r_state);\n";
$res .= "\tif (req == NULL) {\n";
$res .= "\t\treturn NULL;\n";
$res .= "\t}\n";
$res .= "\n";
my $out_params = 0;
foreach (@{$fn->{ELEMENTS}}) {
if (grep(/out/, @{$_->{DIRECTION}})) {
$out_params++;
}
}
my $submem;
if ($out_params > 0) {
$res .= "\tstate->out_mem_ctx = talloc_new(state);\n";
$res .= "\tif (tevent_req_nomem(state->out_mem_ctx, req)) {\n";
$res .= "\t\treturn tevent_req_post(req, ev);\n";
$res .= "\t}\n";
$res .= "\n";
$submem = "state->out_mem_ctx";
} else {
$res .= "\tstate->out_mem_ctx = NULL;\n";
$submem = "state";
}
$res .= "\tsubreq = dcerpc_$name\_send(p, $submem, r);\n";
$res .= "\tif (tevent_req_nomem(subreq, req)) {\n";
$res .= "\t\treturn tevent_req_post(req, ev);\n";
$res .= "\t}\n";
$res .= "\tsubreq->async.callback = dcerpc_$name\_r_done;\n";
$res .= "\tsubreq->async.private_data = req;\n";
$res .= "\n";
$res .= "\treturn req;\n";
$res .= "}\n";
$res .= "\n";
}
sub ParseFunction_r_Done($$$)
{
my ($interface, $fn, $name) = @_;
my $uname = uc $name;
if (has_property($fn, "todo")) {
return;
}
my $proto = "static void dcerpc_$name\_r_done(struct rpc_request *subreq)";
$res .= "$proto\n";
$res .= "{\n";
$res .= "\tstruct tevent_req *req =\n";
$res .= "\t\ttalloc_get_type_abort(subreq->async.private_data,\n";
$res .= "\t\tstruct tevent_req);\n";
$res .= "\tNTSTATUS status;\n";
$res .= "\n";
$res .= "\tstatus = dcerpc_$name\_recv(subreq);\n";
$res .= "\tif (!NT_STATUS_IS_OK(status)) {\n";
$res .= "\t\ttevent_req_nterror(req, status);\n";
$res .= "\t\treturn;\n";
$res .= "\t}\n";
$res .= "\n";
$res .= "\ttevent_req_done(req);\n";
$res .= "}\n";
$res .= "\n";
}
sub ParseFunction_r_Recv($$$)
{
my ($interface, $fn, $name) = @_;
my $uname = uc $name;
if (has_property($fn, "todo")) {
return;
}
my $proto = "NTSTATUS dcerpc_$name\_r_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)";
$res_hdr .= "\n$proto;\n";
$res .= "$proto\n{\n";
$res .= "\tstruct dcerpc_$name\_r_state *state =\n";
$res .= "\t\ttevent_req_data(req,\n";
$res .= "\t\tstruct dcerpc_$name\_r_state);\n";
$res .= "\tNTSTATUS status;\n";
$res .= "\n";
$res .= "\tif (tevent_req_is_nterror(req, &status)) {\n";
$res .= "\t\ttevent_req_received(req);\n";
$res .= "\t\treturn status;\n";
$res .= "\t}\n";
$res .= "\n";
$res .= "\ttalloc_steal(mem_ctx, state->out_mem_ctx);\n";
$res .= "\n";
$res .= "\ttevent_req_received(req);\n";
$res .= "\treturn NT_STATUS_OK;\n";
$res .= "}\n";
$res .= "\n";
}
sub ParseFunction_r_Sync($$$)
{
my ($interface, $fn, $name) = @_;
my $uname = uc $name;
if (has_property($fn, "todo")) {
return;
}
my $proto = "NTSTATUS dcerpc_$name\_r(struct dcerpc_binding_handle *h, TALLOC_CTX *mem_ctx, struct $name *r)";
$res_hdr .= "\n$proto;\n";
$res .= "$proto\n{\n";
$res .= "\tstruct dcerpc_pipe *p =\n";
$res .= "\t\ttalloc_get_type_abort(h->private_data,\n";
$res .= "\t\tstruct dcerpc_pipe);\n";
$res .= "\n";
$res .= "\treturn dcerpc_$name(p, mem_ctx, r);\n";
$res .= "}\n";
$res .= "\n";
}
#####################################################################
# parse a function
sub ParseFunction($$)
{
my ($interface, $fn) = @_;
ParseFunctionSync($interface, $fn, $fn->{NAME});
ParseFunctionOldSend($interface, $fn, $fn->{NAME});
ParseFunctionOldRecv($interface, $fn, $fn->{NAME});
ParseFunction_r_State($interface, $fn, $fn->{NAME});
ParseFunction_r_Send($interface, $fn, $fn->{NAME});
ParseFunction_r_Done($interface, $fn, $fn->{NAME});
ParseFunction_r_Recv($interface, $fn, $fn->{NAME});
ParseFunction_r_Sync($interface, $fn, $fn->{NAME});
}
my %done;
#####################################################################
# parse the interface definitions
sub ParseInterface($)
{
my($interface) = shift;
$res_hdr .= "#ifndef _HEADER_RPC_$interface->{NAME}\n";
$res_hdr .= "#define _HEADER_RPC_$interface->{NAME}\n\n";
if (defined $interface->{PROPERTIES}->{uuid}) {
$res_hdr .= "extern const struct ndr_interface_table ndr_table_$interface->{NAME};\n";
}
$res .= "/* $interface->{NAME} - client functions generated by pidl */\n\n";
foreach my $fn (@{$interface->{FUNCTIONS}}) {
next if not defined($fn->{OPNUM});
next if defined($done{$fn->{NAME}});
ParseFunction($interface, $fn);
$done{$fn->{NAME}} = 1;
}
$res_hdr .= "#endif /* _HEADER_RPC_$interface->{NAME} */\n";
return $res;
}
sub Parse($$$$)
{
my($ndr,$header,$ndr_header,$client_header) = @_;
$res = "";
$res_hdr = "";
$res .= "/* client functions auto-generated by pidl */\n";
$res .= "\n";
if (is_intree()) {
$res .= "#include \"includes.h\"\n";
} else {
$res .= "#ifndef _GNU_SOURCE\n";
$res .= "#define _GNU_SOURCE\n";
$res .= "#endif\n";
$res .= "#include <stdio.h>\n";
$res .= "#include <stdbool.h>\n";
$res .= "#include <stdlib.h>\n";
$res .= "#include <stdint.h>\n";
$res .= "#include <stdarg.h>\n";
$res .= "#include <core/ntstatus.h>\n";
}
$res .= "#include <tevent.h>\n";
$res .= "#include \"lib/util/tevent_ntstatus.h\"\n";
$res .= "#include \"$ndr_header\"\n";
$res .= "#include \"$client_header\"\n";
$res .= "\n";
$res_hdr .= choose_header("librpc/rpc/dcerpc.h", "dcerpc.h")."\n";
$res_hdr .= "#include \"$header\"\n";
foreach my $x (@{$ndr}) {
($x->{TYPE} eq "INTERFACE") && ParseInterface($x);
}
return ($res,$res_hdr);
}
1;
|