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
|
###################################################
# parser generator for IDL structures
# Copyright tpot@samba.org 2001
# Copyright tridge@samba.org 2000
# released under the GNU GPL
package IdlEParser;
use strict;
use dump;
use Data::Dumper;
my($name);
sub ParamSimpleNdrType($)
{
my($p) = shift;
my($res);
$res .= "\toffset = dissect_ndr_$p->{TYPE}(tvb, offset, pinfo, tree, drep, hf_$p->{NAME}, NULL);\n";
return $res;
}
sub ParamPolicyHandle($)
{
my($p) = shift;
my($res);
$res .= "\toffset = dissect_nt_policy_hnd(tvb, offset, pinfo, tree, drep, hf_policy_hnd, NULL, NULL, FALSE, FALSE);\n";
return $res;
}
my %param_handlers = (
'uint16' => \&ParamSimpleNdrType,
'uint32' => \&ParamSimpleNdrType,
'policy_handle' => \&ParamPolicyHandle,
);
#####################################################################
# parse a function
sub ParseParameter($)
{
my($p) = shift;
my($res);
if (defined($param_handlers{$p->{TYPE}})) {
$res .= &{$param_handlers{$p->{TYPE}}}($p);
return $res;
}
$res .= "\t/* Unhandled IDL type '$p->{TYPE}' in $p->{PARENT}->{NAME} */\n";
return $res;
# exit(1);
}
#####################################################################
# parse a function
sub ParseFunction($)
{
my($f) = shift;
my($res);
$res .= "/*\n\n";
$res .= IdlDump::DumpFunction($f);
$res .= "*/\n\n";
# Request function
$res .= "static int\n";
$res .= "$f->{NAME}_rqst(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)\n";
$res .= "{\n";
my($d);
foreach $d (@{$f->{DATA}}) {
$res .= ParseParameter($d), if defined($d->{PROPERTIES}{in});
}
$res .= "\treturn offset;\n";
$res .= "}\n\n";
# Response function
$res .= "static int\n";
$res .= "$f->{NAME}_resp(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)\n";
$res .= "{\n";
foreach $d (@{$f->{DATA}}) {
$res .= ParseParameter($d), if defined($d->{PROPERTIES}{out});
}
$res .= "\n";
$res .= "\treturn offset;\n";
$res .= "}\n\n";
return $res;
}
#####################################################################
# parse the interface definitions
sub Pass2Interface($)
{
my($interface) = shift;
my($data) = $interface->{DATA};
my($res) = "";
foreach my $d (@{$data}) {
$res .= ParseFunction($d), if $d->{TYPE} eq "FUNCTION";
}
return $res;
}
#####################################################################
# Pass 1: Stuff required before structs and functions
sub Pass1ModuleHeader($)
{
my($d) = shift;
my($res) = "";
$res .= << "EOF";
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "packet-dcerpc.h"
#include "packet-dcerpc-nt.h"
EOF
# UUID
if ($d->{TYPE} eq "MODULEHEADER" and defined($d->{PROPERTIES}->{uuid})) {
my $uuid = $d->{PROPERTIES}->{uuid};
$res .= "static e_uuid_t uuid_dcerpc_$name = {\n";
$res .= "\t0x" . substr($uuid, 0, 8);
$res .= ", 0x" . substr($uuid, 9, 4);
$res .= ", 0x" . substr($uuid, 14, 4) . ",\n";
$res .= "\t{ 0x" . substr($uuid, 19, 2);
$res .= ", 0x" . substr($uuid, 21, 2);
$res .= ", 0x" . substr($uuid, 24, 2);
$res .= ", 0x" . substr($uuid, 26, 2);
$res .= ", 0x" . substr($uuid, 28, 2);
$res .= ", 0x" . substr($uuid, 30, 2);
$res .= ", 0x" . substr($uuid, 32, 2);
$res .= ", 0x" . substr($uuid, 34, 2) . " }\n";
$res .= "};\n\n";
$res .= "static guint16 ver_dcerpc_samr = " .
$d->{PROPERTIES}->{version} . ";\n\n";
}
return $res;
}
sub Pass1Interface($)
{
my($interface) = shift;
my($res) = "";
$res .= << "EOF";
static int proto_dcerpc_$name = -1;
static int hf_${name}_opnum = -1;
static int hf_${name}_rc = -1;
static int hf_policy_hnd = -1;
static gint ett_dcerpc_$name = -1;
EOF
my %p = ();
foreach my $fn (@{$interface->{DATA}}) {
next, if $fn->{TYPE} ne "FUNCTION";
foreach my $args ($fn->{DATA}) {
foreach my $params (@{$args}) {
$res .= "static int hf_$params->{NAME} = -1;\n",
if not defined $p{$params->{NAME}};
$p{$params->{NAME}} = 1;
}
}
}
$res .= "\n";
return $res;
}
#####################################################################
# Pass 3: trailing stuff
sub Pass3Interface($)
{
my($interface) = shift;
my($res) = "";
$res .= "static dcerpc_sub_dissector dcerpc_${name}_dissectors[] = {\n";
my $num = 0;
foreach my $d (@{$interface->{DATA}}) {
if ($d->{TYPE} eq "FUNCTION") {
# Strip module name from function name, if present
my $n = $d->{NAME};
$n = substr($d->{NAME}, length($name) + 1),
if $name eq substr($d->{NAME}, 0, length($name));
$res .= "\t{ $num, \"$n\",\n";
$res .= "\t\t$d->{NAME}_rqst,\n";
$res .= "\t\t$d->{NAME}_resp },\n";
$num++;
}
}
$res .= "};\n\n";
return $res;
}
#####################################################################
# parse a parsed IDL structure back into an IDL file
sub Parse($)
{
my($idl) = shift;
my($res) = "/* parser auto-generated by pidl */\n\n";
my($d);
# Pass 0: set module name
foreach $d (@{$idl}) {
$name = $d->{NAME}, if ($d->{TYPE} eq "INTERFACE");
}
# Pass 1: header stuff
foreach $d (@{$idl}) {
$res .= Pass1ModuleHeader($d), if $d->{TYPE} eq "MODULEHEADER";
$res .= Pass1Interface($d), if $d->{TYPE} eq "INTERFACE";
}
# Pass 2: typedefs and functions
foreach $d (@{$idl}) {
$res .= Pass2Interface($d), if $d->{TYPE} eq "INTERFACE";
}
# Pass 3: trailing stuff
foreach $d (@{$idl}) {
$res .= Pass3Interface($d), if $d->{TYPE} eq "INTERFACE";
}
$res .= << "EOF";
void
proto_reg_handoff_dcerpc_$name(void)
{
dcerpc_init_uuid(proto_dcerpc_$name, ett_dcerpc_$name,
&uuid_dcerpc_$name, ver_dcerpc_$name,
dcerpc_${name}_dissectors, hf_${name}_opnum);
}
EOF
return $res;
}
1;
|