blib/lib/DCE/Perl/RPC.pm

filter: Statementstmt 96.6% 204 / 211 Branchbran 68.7% 11 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 97.7% 43 / 44 Podpod 0.0% 0 / 8 totaltotal 91.2% 259 / 284 CC 10 SCAR 4.4
CC
10
Cov
94%
rpc_co_request
4.0
rpc_bind
3.0
rpc_alt_ctx
3.0
CRAP 1.6
↑ Summary
1 # -*- perl -*-
2 # RPC.pm - An implementation of a DCE RPC Composer/Parser. It is expected
3 # to cover all the connection oriented PDUs.
4 # implemented the client side functions that calculates the NTLM response.
5 # I will add the corresponding server side functions in the next version.
6 #
7
8 package DCE::Perl::RPC;
9
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 1 require Exporter;
15 1 require DynaLoader;
16
17 1 *import = \&Exporter::import;
18
19 1 @ISA = qw (Exporter DynaLoader);
20 1 @EXPORT = qw ();
21 1 @EXPORT_OK = qw ();
22 1 $VERSION = '0.01';
23
24 # Stolen from Crypt::DES.
25 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
Pod undocumented
26 0 my ($package, $filename, $line, $subr) = caller (1);
27 0 $Carp::CarpLevel = 2;
28 0 croak "Usage: $subr (@_)";
29 }
30
31 # DCE RPC PDU Types
32 1 ▶use constant RPC_REQUEST => 0x00;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use constant RPC_PING => 0x01;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use constant RPC_RESPONSE => 0x02;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use constant RPC_FAULT => 0x03;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant RPC_WORKING => 0x04;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use constant RPC_NOCALL => 0x05;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use constant RPC_REJECT => 0x06;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use constant RPC_ACK => 0x07;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use constant RPC_CL_CANCEL => 0x08;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use constant RPC_FACK => 0x09;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use constant RPC_CANCEL_ACK => 0x0a;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use constant RPC_BIND => 0x0b;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use constant RPC_BIND_ACK => 0x0c;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use constant RPC_BIND_NACK => 0x0d;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use constant RPC_ALTER_CONTEXT => 0x0e;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use constant RPC_ALTER_CONTEXT_RESP => 0x0f;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use constant RPC_BIND_RESP => 0x10;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use constant RPC_SHUTDOWN => 0x11;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use constant RPC_CO_CANCEL => 0x12;
Subroutine BEGIN: called CC=1 SCAR=0.0
51
52 # DCE RPC PFC Flags
53 # First Fragment
54 1 ▶use constant PFC_FIRST_FRAG => 0x01;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 # Last Fragment
56 1 ▶use constant PFC_LAST_FRAG => 0x02;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 # Cancel was pending at sender
58 1 ▶use constant PFC_PENDING_CANCEL => 0x04;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 # Reserved
60 1 ▶use constant PFC_RESERVED_1 => 0x08;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 # supports concurrent multiplexing of a single connection
62 1 ▶use constant PFC_CONC_MPX => 0x10;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 # only meaningful on 'fault' packet; if true, guaranteed call
64 # did not execute
65 1 ▶use constant PFC_DID_NOT_EXECUTE => 0x20;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 # 'maybe' call semantics requested
67 1 ▶use constant PFC_MAYBE => 0x40;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 # if true, a non-nul object UUID was specified in the handle,
69 # and is present in the optional object field. If false, the
70 # object field is omitted.
71 1 ▶use constant PFC_OBJECT_UUID => 0x80;
Subroutine BEGIN: called CC=1 SCAR=0.0
72
73 1 ▶use constant RPC_MAJOR_VERSION => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 ▶use constant RPC_MINOR_VERSION => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
75
76 # Connection Oriented PDU common header size
77 1 ▶use constant RPC_CO_HDR_SZ => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
78
79 # Fragment Size
80 1 ▶use constant RPC_FRAG_SZ => 5840;
Subroutine BEGIN: called CC=1 SCAR=0.0
81
82 1 ▶use constant RPC_AUTH_NTLM => 0x0a;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶use constant RPC_AUTH_LEVEL_CONNECT => 0x02;
Subroutine BEGIN: called CC=1 SCAR=0.0
84
85 #########################################################################
86 # Constructor to initialize authentication related information. In this #
87 # version, we assume NTLM as the authentication scheme of choice. #
88 # The constructor only takes the class name as an argument. #
89 #########################################################################
90 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
91 1 ▶ usage("new DCE::Perl::RPC") unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 1 1
92 1 my ($package) = @_;
93 1 srand time;
94 1 my $ctx_id = pack("V", rand 2**32);
95 1 bless {'auth_type' => RPC_AUTH_NTLM,
96 'auth_level' => RPC_AUTH_LEVEL_CONNECT,
97 'auth_ctx_id' => $ctx_id}, $package;
98 }
99
100 ############################################################################
101 # rpc_co_hdr composes the 16-bytes common DCE RPC header that must present #
102 # in all conection oriented DCE RPC messages. It takes four arguments: #
103 # 1) PDU type; 2) PDU flags; 3) size of the PDU part that is specific to #
104 # the PDU type; 4) size of the authentication credentials. #
105 # This function is an internal function. It is not supposed to be called #
106 # from the outside world. #
107 ############################################################################
108 sub rpc_co_hdr($$$$)
109 4 ▶{
Subroutine rpc_co_hdr: called CC=1 SCAR=0.0
Pod undocumented
110 4 my ($type, $flags, $size, $auth_size) = @_;
111 4 my $msg = chr(RPC_MAJOR_VERSION) . chr(RPC_MINOR_VERSION);
112 4 $msg .= chr($type);
113 4 $msg .= chr($flags);
114 4 $msg .= pack("H8", "10000000"); # assume little endian
115 4 $msg .= pack("v", RPC_CO_HDR_SZ+$size+$auth_size);
116 4 $msg .= pack("v", $auth_size);
117 4 $msg .= pack("V", 0x00); # always 0 for call_id for now
118 4 return $msg;
119 }
120
121 ############################################################################
122 # rpc_auth_hdr composes the 8-bytes authentication header. It takes four #
123 # arguments: 1) Authentication Type; 2) Authentication Level; 3) length of #
124 # padding; 4) context id of this session. #
125 ############################################################################
126 sub rpc_auth_hdr($$$$)
127 3 ▶{
Subroutine rpc_auth_hdr: called CC=1 SCAR=0.0
Pod undocumented
128 3 my ($auth_type, $auth_level, $pad_len, $ctx_id) = @_;
129 3 my $msg = chr($auth_type);
130 3 $msg .= chr($auth_level);
131 3 $msg .= chr($pad_len);
132 3 $msg .= chr(0);
133 3 $msg .= $ctx_id;
134 3 return $msg;
135 }
136
137 #####################################################################
138 # rpc_bind composes the DCE RPC bind PDU. To make things simple, it #
139 # assumes the PDU context list only has one element. It takes four #
140 # arguments: 1) Presentation Context Id; 2) Abstract Syntax #
141 # concatenated with interface version; 3) list of transfer syntax #
142 # concatenated with interface version; 4) authentication #
143 # credentials. #
144 #####################################################################
145 sub rpc_bind($$$@$)
146 1 ▶{
Subroutine rpc_bind: called CC=3 SCAR=11.0
Pod undocumented
147 1 my $self = shift;
148 1 my $ctx_id = shift;
149 1 my $abs_syntax = shift;
150 1 my @xfer_syntax = shift;
151 1 my $auth_value = shift;
152 1 my $msg = "";
153 1 my $auth_pad = 0;
154 1 my $i;
155 1 my $bind_msg = pack("v", RPC_FRAG_SZ) . pack("v", RPC_FRAG_SZ);
156 1 $bind_msg .= pack("V", 0); # ask for new association group id
157 1 $bind_msg .= chr(1) . chr(0) . pack("v", 0);
158 1 $bind_msg .= pack("v", $ctx_id); # ctx id
159 1 $bind_msg .= chr(@xfer_syntax);
160 1 $bind_msg .= chr(0);
161 1 $bind_msg .= $abs_syntax;
162 1 ▶ for ($i = 0; $i < @xfer_syntax; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @xfer_syntax) 1 1 2
163 1 $bind_msg .= $xfer_syntax[$i];
164 }
165 1 ▶ while (length($bind_msg) % 4 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($bind_msg) % 4 != 0) 0 1 1
166 0 $bind_msg .= chr(0);
167 0 $auth_pad++;
168 }
169 1 $bind_msg .= rpc_auth_hdr($self->{'auth_type'}, $self->{'auth_level'}, $auth_pad, $self->{'auth_ctx_id'});
170 1 $msg = rpc_co_hdr(RPC_BIND, PFC_FIRST_FRAG | PFC_LAST_FRAG,
171 length($bind_msg), length($auth_value)) . $bind_msg . $auth_value;
172 1 return $msg;
173 }
174
175 ##############################################################################
176 # rpc_bind_resp composes the DCE RPC bind_resp PDU. This PDU is undocumented #
177 # in the OpenGroup's specification but it is used by DCOM. It's main #
178 # responsibility is to respond to the NTLM challenge posted by the bind_ack #
179 # PDU from the server. Its lone argument is the NTLM response. #
180 ##############################################################################
181 sub rpc_bind_resp($$)
182 1 ▶{
Subroutine rpc_bind_resp: called CC=2 SCAR=7.1
Pod undocumented
183 1 my $self = shift;
184 1 my $auth_value = shift;
185 1 my $msg = "";
186 1 my $auth_pad = 0;
187 1 my $i;
188 1 my $bind_resp_msg = pack("v", RPC_FRAG_SZ) . pack("v", RPC_FRAG_SZ);
189 1 ▶ while (length($bind_resp_msg) % 4 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($bind_resp_msg) % 4 != 0) 0 1 1
190 0 $bind_resp_msg .= chr(0);
191 0 $auth_pad++;
192 }
193 1 $bind_resp_msg .= rpc_auth_hdr($self->{'auth_type'}, $self->{'auth_level'}, $auth_pad, $self->{'auth_ctx_id'});
194 1 $msg = rpc_co_hdr(RPC_BIND_RESP, PFC_FIRST_FRAG | PFC_LAST_FRAG,
195 length($bind_resp_msg), length($auth_value)) . $bind_resp_msg . $auth_value;
196 1 return $msg;
197 }
198
199 ###########################################################################
200 # rpc_co_request composes the connection-oriented DCE RPC Request PDU. It #
201 # takes five arguments: 1) the stub; 2) the presentation context id; #
202 # 3) operation # within the interface; 4) object UUID; 5) authetication #
203 # credentials. The fourth argument can be "" if there is no UUID #
204 # associate with this request PDU. #
205 ###########################################################################
206 sub rpc_co_request($$$$$$)
207 1 ▶{
Subroutine rpc_co_request: called CC=4 SCAR=13.9
Pod undocumented
208 1 my ($self, $body, $ctx_id, $op_num, $uuid, $auth_value) = @_;
209 1 my $msg = "";
210 1 my $auth_pad = 0;
211 1 my $i;
212 1 my $flags = PFC_FIRST_FRAG | PFC_LAST_FRAG;
213 1 my $req_msg = pack("V", length($body));
214 1 $req_msg .= pack("v", $ctx_id);
215 1 $req_msg .= pack("v", $op_num);
216 1 ▶ if (defined($uuid) and length($uuid) == 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uuid and length $uuid == 16) 1 0 1
Condition33%1/3
defined $uuid and length $uuid == 16
!ll&&!rl&&r
001
MC/DC0%0/2
defined $uuid and length $uuid == 16
defined $uuid length $uuid == 16
Truth table33%1/3
A and B
A= defined $uuidB= length $uuid == 16
ABresult
0X0
100
111
217 1 $flags |= PFC_OBJECT_UUID;
218 1 $req_msg .= $uuid;
219 }
220 1 $req_msg .= $body;
221 1 ▶ while (length($req_msg) % 4 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($req_msg) % 4 != 0) 2 1 3
222 2 $req_msg .= chr(0);
223 2 $auth_pad++;
224 }
225 1 $req_msg .= rpc_auth_hdr($self->{'auth_type'}, $self->{'auth_level'}, $auth_pad, $self->{'auth_ctx_id'});
226 1 $msg = rpc_co_hdr(RPC_REQUEST, $flags,
227 length($req_msg), length($auth_value)) . $req_msg . $auth_value;
228 1 return $msg;
229 }
230
231 ##########################################################################
232 # rpc_alt_ctx composes a DCE RPC alter_context PDU. alter_context PDU is #
233 # used to change the presentation syntax established by the earlier bind #
234 # PDU. Therefore it has similar format. However, there is no need for #
235 # authentication credentials. Like rpc_bind, we also assume the #
236 # presentation context list only has one element. #
237 ##########################################################################
238 sub rpc_alt_ctx($$$@)
239 1 ▶{
Subroutine rpc_alt_ctx: called CC=3 SCAR=11.0
Pod undocumented
240 1 my $self = shift;
241 1 my $ctx_id = shift;
242 1 my $abs_syntax = shift;
243 1 ▶ usage("Abstract Syntax plus interface version must be 20-bytes long!") unless length($abs_syntax) == 20;
Branch: 1 branch
BranchTrueFalseTotal
unless length $abs_syntax == 20 0 1 1
244 1 my @xfer_syntax = shift;
245 1 my $msg = "";
246 1 my $i;
247 1 my $alt_ctx_msg = pack("v", RPC_FRAG_SZ) . pack("v", RPC_FRAG_SZ);
248 1 $alt_ctx_msg .= pack("V", 0); # ask for new association group id
249 1 $alt_ctx_msg .= chr(1) . chr(0) . pack("v", 0);
250 1 $alt_ctx_msg .= pack("v", $ctx_id); # ctx id
251 1 $alt_ctx_msg .= chr(@xfer_syntax);
252 1 $alt_ctx_msg .= chr(0);
253 1 $alt_ctx_msg .= $abs_syntax;
254 1 ▶ for ($i = 0; $i < @xfer_syntax; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @xfer_syntax) 1 1 2
255 1 $alt_ctx_msg .= $xfer_syntax[$i];
256 }
257 1 $msg = rpc_co_hdr(RPC_ALTER_CONTEXT, PFC_FIRST_FRAG | PFC_LAST_FRAG,
258 length($alt_ctx_msg), 0) . $alt_ctx_msg;
259 1 return $msg;
260 }
261
262 1 1;
263
264 __END__
↑ Summary