blib/lib/DBI/ProxyServer.pm untested

filter: Statementstmt 0.0% 0 / 166 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 255 CC 16 SCAR 31.0
CC
16
Cov
0%
prepare
56.0
execute
42.0
AcceptUser
12.0
CRAP 22.1
1 #!/usr/bin/perl
2 # $Header: /home/timbo/dbi/lib/DBI/RCS/ProxyServer.pm,v 11.9 2003/05/14 11:08:17 timbo Exp $
3 # -*- perl -*-
4 #
5 # DBI::ProxyServer - a proxy server for DBI drivers
6 #
7 # Copyright (c) 1997 Jochen Wiedmann
8 #
9 # The DBD::Proxy module is free software; you can redistribute it and/or
10 # modify it under the same terms as Perl itself. In particular permission
11 # is granted to Tim Bunce for distributing this as a part of the DBI.
12 #
13 #
14 # Author: Jochen Wiedmann
15 # Am Eisteich 9
16 # 72555 Metzingen
17 # Germany
18 #
19 # Email: joe@ispsoft.de
20 # Phone: +49 7123 14881
21 #
22 #
23 ##############################################################################
24  
25 use strict;
26 use warnings;
27  
28 use RPC::PlServer 0.2001;
29 require DBI;
30 require Config;
31  
32 package DBI::ProxyServer;
33  
34 ############################################################################
35 #
36 # Constants
37 #
38 ############################################################################
39  
40 our $VERSION = "0.3005";
41 our @ISA = qw(RPC::PlServer DBI);
42  
43 # Most of the options below are set to default values, we note them here
44 # just for the sake of documentation.
45 my %DEFAULT_SERVER_OPTIONS;
46 {
47 my $o = \%DEFAULT_SERVER_OPTIONS;
48 $o->{'chroot'} = undef, # To be used in the initfile,
49 # after loading the required
50 # DBI drivers.
51 $o->{'clients'} =
52 [ { 'mask' => '.*',
53 'accept' => 1,
54 'cipher' => undef
55 }
56 ];
57 $o->{'configfile'} = '/etc/dbiproxy.conf' if -f '/etc/dbiproxy.conf';
58 $o->{'debug'} = 0;
59 $o->{'facility'} = 'daemon';
60 $o->{'group'} = undef;
61 $o->{'localaddr'} = undef; # Bind to any local IP number
62 $o->{'localport'} = undef; # Must set port number on the
63 # command line.
64 $o->{'logfile'} = undef; # Use syslog or EventLog.
65  
66 # XXX don't restrict methods that can be called (trust users once connected)
67 $o->{'XXX_methods'} = {
68 'DBI::ProxyServer' => {
69 'Version' => 1,
70 'NewHandle' => 1,
71 'CallMethod' => 1,
72 'DestroyHandle' => 1
73 },
74 'DBI::ProxyServer::db' => {
75 'prepare' => 1,
76 'commit' => 1,
77 'rollback' => 1,
78 'STORE' => 1,
79 'FETCH' => 1,
80 'func' => 1,
81 'quote' => 1,
82 'type_info_all' => 1,
83 'table_info' => 1,
84 'disconnect' => 1,
85 },
86 'DBI::ProxyServer::st' => {
87 'execute' => 1,
88 'STORE' => 1,
89 'FETCH' => 1,
90 'func' => 1,
91 'fetch' => 1,
92 'finish' => 1
93 }
94 };
95 if ($Config::Config{'usethreads'} eq 'define') {
96 $o->{'mode'} = 'threads';
97 } elsif ($Config::Config{'d_fork'} eq 'define') {
98 $o->{'mode'} = 'fork';
99 } else {
100 $o->{'mode'} = 'single';
101 }
102 # No pidfile by default, configuration must provide one if needed
103 $o->{'pidfile'} = 'none';
104 $o->{'user'} = undef;
105 };
106  
107  
108 ############################################################################
109 #
110 # Name: Version
111 #
112 # Purpose: Return version string
113 #
114 # Inputs: $class - This class
115 #
116 # Result: Version string; suitable for printing by "--version"
117 #
118 ############################################################################
119  
120 sub Version {
121 my $version = $DBI::ProxyServer::VERSION;
122 "DBI::ProxyServer $version, Copyright (C) 1998, Jochen Wiedmann";
123 }
124  
125  
126 ############################################################################
127 #
128 # Name: AcceptApplication
129 #
130 # Purpose: Verify DBI DSN
131 #
132 # Inputs: $self - This instance
133 # $dsn - DBI dsn
134 #
135 # Returns: TRUE for a valid DSN, FALSE otherwise
136 #
137 ############################################################################
138  
139 sub AcceptApplication {
140 my $self = shift; my $dsn = shift;
141 $dsn =~ /^dbi:\w+:/i;
142 }
143  
144  
145 ############################################################################
146 #
147 # Name: AcceptVersion
148 #
149 # Purpose: Verify requested DBI version
150 #
151 # Inputs: $self - Instance
152 # $version - DBI version being requested
153 #
154 # Returns: TRUE for ok, FALSE otherwise
155 #
156 ############################################################################
157  
158 sub AcceptVersion {
159 my $self = shift; my $version = shift;
160 require DBI;
161 DBI::ProxyServer->init_rootclass();
162 $DBI::VERSION >= $version;
163 }
164  
165  
166 ############################################################################
167 #
168 # Name: AcceptUser
169 #
170 # Purpose: Verify user and password by connecting to the client and
171 # creating a database connection
172 #
173 # Inputs: $self - Instance
174 # $user - User name
175 # $password - Password
176 #
177 ############################################################################
178  
179 sub AcceptUser {
180 my $self = shift; my $user = shift; my $password = shift;
181 return 0 if (!$self->SUPER::AcceptUser($user, $password));
182 my $dsn = $self->{'application'};
183 $self->Debug("Connecting to $dsn as $user");
184 local $ENV{DBI_AUTOPROXY} = ''; # :-)
185 $self->{'dbh'} = eval {
186 DBI::ProxyServer->connect($dsn, $user, $password,
187 { 'PrintError' => 0,
188 'Warn' => 0,
189 'RaiseError' => 1,
190 'HandleError' => sub {
191 my $err = $_[1]->err;
192 my $state = $_[1]->state || '';
193 $_[0] .= " [err=$err,state=$state]";
194 return 0;
195 } })
196 };
197 if ($@) {
198 $self->Error("Error while connecting to $dsn as $user: $@");
199 return 0;
200 }
201 [1, $self->StoreHandle($self->{'dbh'}) ];
202 }
203  
204  
205 sub CallMethod {
206 my $server = shift;
207 my $dbh = $server->{'dbh'};
208 # We could store the private_server attribute permanently in
209 # $dbh. However, we'd have a reference loop in that case and
210 # I would be concerned about garbage collection. :-(
211 $dbh->{'private_server'} = $server;
212 $server->Debug("CallMethod: => " . do { no warnings; join(",", @_)});
213 my @result = eval { $server->SUPER::CallMethod(@_) };
214 my $msg = $@;
215 undef $dbh->{'private_server'};
216 if ($msg) {
217 $server->Debug("CallMethod died with: $@");
218 die $msg;
219 } else {
220 $server->Debug("CallMethod: <= " . do { no warnings; join(",", @result) });
221 }
222 @result;
223 }
224  
225  
226 sub main {
227 my $server = DBI::ProxyServer->new(\%DEFAULT_SERVER_OPTIONS, \@_);
228 $server->Bind();
229 }
230  
231  
232 ############################################################################
233 #
234 # The DBI part of the proxyserver is implemented as a DBI subclass.
235 # Thus we can reuse some of the DBI methods and overwrite only
236 # those that need additional handling.
237 #
238 ############################################################################
239  
240 package DBI::ProxyServer::dr;
241  
242 our @ISA = qw(DBI::dr);
243  
244  
245 package DBI::ProxyServer::db;
246  
247 our @ISA = qw(DBI::db);
248  
249 sub prepare {
250 my($dbh, $statement, $attr, $params, $proto_ver) = @_;
251 my $server = $dbh->{'private_server'};
252 if (my $client = $server->{'client'}) {
253 if ($client->{'sql'}) {
254 if ($statement =~ /^\s*(\S+)/) {
255 my $st = $1;
256 if (!($statement = $client->{'sql'}->{$st})) {
257 die "Unknown SQL query: $st";
258 }
259 } else {
260 die "Cannot parse restricted SQL statement: $statement";
261 }
262 }
263 }
264 my $sth = $dbh->SUPER::prepare($statement, $attr);
265 my $handle = $server->StoreHandle($sth);
266  
267 if ( $proto_ver and $proto_ver > 1 ) {
268 $sth->{private_proxyserver_described} = 0;
269 return $handle;
270  
271 } else {
272 # The difference between the usual prepare and ours is that we implement
273 # a combined prepare/execute. The DBD::Proxy driver doesn't call us for
274 # prepare. Only if an execute happens, then we are called with method
275 # "prepare". Further execute's are called as "execute".
276 my @result = $sth->execute($params);
277 my ($NAME, $TYPE);
278 my $NUM_OF_FIELDS = $sth->{NUM_OF_FIELDS};
279 if ($NUM_OF_FIELDS) { # is a SELECT
280 $NAME = $sth->{NAME};
281 $TYPE = $sth->{TYPE};
282 }
283 ($handle, $NUM_OF_FIELDS, $sth->{'NUM_OF_PARAMS'},
284 $NAME, $TYPE, @result);
285 }
286 }
287  
288 sub table_info {
289 my $dbh = shift;
290 my $sth = $dbh->SUPER::table_info();
291 my $numFields = $sth->{'NUM_OF_FIELDS'};
292 my $names = $sth->{'NAME'};
293 my $types = $sth->{'TYPE'};
294  
295 # We wouldn't need to send all the rows at this point, instead we could
296 # make use of $rsth->fetch() on the client as usual.
297 # The problem is that some drivers (namely DBD::ExampleP, DBD::mysql and
298 # DBD::mSQL) are returning foreign sth's here, thus an instance of
299 # DBI::st and not DBI::ProxyServer::st. We could fix this by permitting
300 # the client to execute method DBI::st, but I don't like this.
301 my @rows;
302 while (my ($row) = $sth->fetch()) {
303 last unless defined $row;
304 push(@rows, [@$row]);
305 }
306 ($numFields, $names, $types, @rows);
307 }
308  
309  
310 package DBI::ProxyServer::st;
311  
312 our @ISA = qw(DBI::st);
313  
314 sub execute {
315 my $sth = shift; my $params = shift; my $proto_ver = shift;
316 my @outParams;
317 if ($params) {
318 for (my $i = 0; $i < @$params;) {
319 my $param = $params->[$i++];
320 if (!ref($param)) {
321 $sth->bind_param($i, $param);
322 }
323 else {
324 if (!ref(@$param[0])) {#It's not a reference
325 $sth->bind_param($i, @$param);
326 }
327 else {
328 $sth->bind_param_inout($i, @$param);
329 my $ref = shift @$param;
330 push(@outParams, $ref);
331 }
332 }
333 }
334 }
335 my $rows = $sth->SUPER::execute();
336 if ( $proto_ver and $proto_ver > 1 and not $sth->{private_proxyserver_described} ) {
337 my ($NAME, $TYPE);
338 my $NUM_OF_FIELDS = $sth->{NUM_OF_FIELDS};
339 if ($NUM_OF_FIELDS) { # is a SELECT
340 $NAME = $sth->{NAME};
341 $TYPE = $sth->{TYPE};
342 }
343 $sth->{private_proxyserver_described} = 1;
344 # First execution, we ship back description.
345 return ($rows, $NUM_OF_FIELDS, $sth->{'NUM_OF_PARAMS'}, $NAME, $TYPE, @outParams);
346 }
347 ($rows, @outParams);
348 }
349  
350 sub fetch {
351 my $sth = shift; my $numRows = shift || 1;
352 my($ref, @rows);
353 while ($numRows-- && ($ref = $sth->SUPER::fetch())) {
354 push(@rows, [@$ref]);
355 }
356 @rows;
357 }
358  
359  
360 1;
361  
362  
363 __END__