| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Server::XmlRpc.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: DTA::CAB XML-RPC server using RPC::XML | ||
| 6 | |||
| 7 | package DTA::CAB::Server::XmlRpc; | ||
| 8 | use DTA::CAB::Server; | ||
| 9 | use RPC::XML; | ||
| 10 | use RPC::XML::Server; | ||
| 11 | use Encode qw(encode decode); | ||
| 12 | use Socket qw(SOMAXCONN); | ||
| 13 | use Carp; | ||
| 14 | use strict; | ||
| 15 | |||
| 16 | ##============================================================================== | ||
| 17 | ## Globals | ||
| 18 | ##============================================================================== | ||
| 19 | |||
| 20 | our @ISA = qw(DTA::CAB::Server); | ||
| 21 | |||
| 22 | BEGIN { | ||
| 23 | ##-- RPC::XML::Server v1.48 (kaskade / debian squeeze) DOES have add_proc() but does NOT have add_procedure() | ||
| 24 | ##-- RPC::XML::Server v1.68 (kaskade2 / debian wheezy) does NOT have add_proc() but DOES have add_procedure() | ||
| 25 | if (!RPC::XML::Server->can('add_procedure') && RPC::XML::Server->can('add_proc')) { | ||
| 26 | *RPC::XML::Server::add_procedure = \&RPC::XML::Server::add_proc; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | ##============================================================================== | ||
| 31 | ## Constructors etc. | ||
| 32 | ##============================================================================== | ||
| 33 | |||
| 34 | ## $obj = CLASS_OR_OBJ->new(%args) | ||
| 35 | ## + object structure: HASH ref | ||
| 36 | ## { | ||
| 37 | ## ##-- Underlying server | ||
| 38 | ## xsrv => $xsrv, ##-- low-level server, an RPC::XML::Server object | ||
| 39 | ## xopt => \%opts, ##-- options for RPC::XML::Server->new() | ||
| 40 | ## xrun => \%opts, ##-- options for RPC::XML::Server->server_loop() | ||
| 41 | ## ## | ||
| 42 | ## ##-- XML-RPC procedure naming | ||
| 43 | ## procNamePrefix => $prefix, ##-- default: 'dta.cab.' | ||
| 44 | ## ## | ||
| 45 | ## ##-- hacks | ||
| 46 | ## encoding => $enc, ##-- sets $RPC::XML::ENCODING on prepare(), used by underlying server | ||
| 47 | ## ## | ||
| 48 | ## ##-- security | ||
| 49 | ## allowUserOptions => $bool, ##-- allow user options? (default: true) | ||
| 50 | ## ## | ||
| 51 | ## ##-- logging | ||
| 52 | ## logRegisterProc => $level, ##-- log xml-rpc procedure registration at $level (default='trace') | ||
| 53 | ## logCall => $level, ##-- log client IP and procedure at $level (default='debug') | ||
| 54 | ## logCallData => $bool, ##-- log client data queries at $level (default=undef: none) | ||
| 55 | ## ## | ||
| 56 | ## ##-- (inherited from DTA::CAB::Server) | ||
| 57 | ## as => \%analyzers, ##-- ($name=>$cab_analyzer_obj, ...) | ||
| 58 | ## aos => \%anlOptions, ##-- ($name=>\%analyzeOptions, ...) : %opts passed to $anl->analyzeXYZ($xyz,%opts) | ||
| 59 | ## } | ||
| 60 | sub new { | ||
| 61 | my $that = shift; | ||
| 62 | return $that->SUPER::new( | ||
| 63 | ##-- underlying server | ||
| 64 | xsrv => undef, | ||
| 65 | xopt => { | ||
| 66 | #path => '/', ##-- URI path for underlying server (HTTP::Daemon) | ||
| 67 | #host => '0.0.0.0', ##-- host for underlying server (HTTP::Daemon) | ||
| 68 | port => 8088, ##-- port for underlying server (HTTP::Daemon) | ||
| 69 | queue => SOMAXCONN, ##-- queue size for underlying server (HTTP::Daemon) | ||
| 70 | #timeout => 10, ##-- connection timeout (HTTP::Daemon) | ||
| 71 | ## | ||
| 72 | #no_default => 1, ##-- disable default methods (default=enabled) | ||
| 73 | #auto_methods => 1, ##-- enable auto-method seek (default=0) | ||
| 74 | }, | ||
| 75 | xrun => { | ||
| 76 | #signal => [qw(INT HUP TERM)], | ||
| 77 | signal => 0, ##-- don't catch any signals by default | ||
| 78 | }, | ||
| 79 | ## | ||
| 80 | ##-- XML-RPC procedure naming | ||
| 81 | procNamePrefix => 'dta.cab.', | ||
| 82 | ## | ||
| 83 | ##-- hacks | ||
| 84 | encoding => 'UTF-8', | ||
| 85 | ## | ||
| 86 | ##-- security | ||
| 87 | allowUserOptions => 1, | ||
| 88 | ## | ||
| 89 | ##-- logging | ||
| 90 | logRegisterProc => 'trace', | ||
| 91 | logCall => 'debug', | ||
| 92 | logCallData => undef, | ||
| 93 | ## | ||
| 94 | ##-- user args | ||
| 95 | @_ | ||
| 96 | ); | ||
| 97 | } | ||
| 98 | |||
| 99 | ## undef = $obj->initialize() | ||
| 100 | ## + called to initialize new objects after new() | ||
| 101 | |||
| 102 | ##============================================================================== | ||
| 103 | ## Methods: Encoding Hacks | ||
| 104 | ##============================================================================== | ||
| 105 | |||
| 106 | ## \%rpcProcHash = $srv->wrapMethodEncoding(\%rpcProcHash) | ||
| 107 | ## + wraps an RPC::XML::procedure spec into $srv->{encoding}-safe code, | ||
| 108 | ## only if $rpcProcHash{wrapEncoding} is set to a true value | ||
| 109 | sub wrapMethodEncoding { | ||
| 110 | my $srv = shift; | ||
| 111 | if (defined($srv->{encoding}) && $_[0]{wrapEncoding}) { | ||
| 112 | my $code_orig = $_[0]{code_orig} = $_[0]{code}; | ||
| 113 | $_[0]{code} = sub { | ||
| 114 | my $rv = $code_orig->(@_); | ||
| 115 | my $rve = DTA::CAB::Utils::deep_encode($srv->{encoding}, $rv); | ||
| 116 | return $rve; | ||
| 117 | }; | ||
| 118 | } | ||
| 119 | return $_[0]; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | ##============================================================================== | ||
| 124 | ## Methods: Generic Server API | ||
| 125 | ##============================================================================== | ||
| 126 | |||
| 127 | ## $rc = $srv->prepareLocal() | ||
| 128 | ## + subclass-local initialization | ||
| 129 | sub prepareLocal { | ||
| 130 | my $srv = shift; | ||
| 131 | |||
| 132 | ##-- get RPC::XML object | ||
| 133 | my $xsrv = $srv->{xsrv} = RPC::XML::Server->new(%{$srv->{xopt}}); | ||
| 134 | if (!ref($xsrv)) { | ||
| 135 | $srv->logcroak("could not create underlying RPC::XML::Server object: $xsrv\n"); | ||
| 136 | } | ||
| 137 | |||
| 138 | ##-- hack: set server encoding | ||
| 139 | if (defined($srv->{encoding})) { | ||
| 140 | $srv->info("(hack) setting RPC::XML::ENCODING = $srv->{encoding}"); | ||
| 141 | $RPC::XML::ENCODING = $srv->{encoding}; | ||
| 142 | } | ||
| 143 | ##-- hack: set $RPC::XML::FORCE_STRING_ENCODINTG | ||
| 144 | $srv->info("(hack) setting RPC::XML::FORCE_STRING_ENCODING = 1"); | ||
| 145 | $RPC::XML::FORCE_STRING_ENCODING = 1; | ||
| 146 | |||
| 147 | ##-- register analysis methods | ||
| 148 | my ($aname,$a,$aopts, $xp, $proc); | ||
| 149 | while (($aname,$a)=each(%{$srv->{as}})) { | ||
| 150 | $aopts = $srv->{aos}{$aname}; | ||
| 151 | $aopts = RPC::XML::struct->new($aopts) if ($aopts); | ||
| 152 | foreach ($a->xmlRpcMethods) { | ||
| 153 | if (UNIVERSAL::isa($_,'HASH')) { | ||
| 154 | ##-- hack method 'name' | ||
| 155 | $_->{name} = 'analyze' if (!defined($_->{name})); | ||
| 156 | $_->{name} = $aname.'.'.$_->{name} if ($aname); | ||
| 157 | $_->{name} = $srv->{procNamePrefix}.$_->{name} if ($srv->{procNamePrefix}); | ||
| 158 | $_->{opts} = $aopts; | ||
| 159 | $srv->wrapMethodEncoding($_); ##-- hack encoding? | ||
| 160 | } | ||
| 161 | $xp = DTA::CAB::Server::XmlRpc::Procedure->new($_); | ||
| 162 | $xp = $xsrv->add_method($xp); | ||
| 163 | if (!ref($xp)) { | ||
| 164 | $srv->error("could not register XML-RPC procedure ".(ref($_) ? "$_->{name}()" : "'$_'")." for analyzer '$aname'\n", | ||
| 165 | " + RPC::XML::Server error: $xp\n", | ||
| 166 | ); | ||
| 167 | } else { | ||
| 168 | $srv->vlog($srv->{logRegisterProc},"registered XML-RPC procedure $_->{name}() for analyzer '$aname'\n"); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | ##-- register 'listAnalyzers' method | ||
| 174 | my $listproc = $srv->listAnalyzersProc; | ||
| 175 | $xsrv->add_procedure( DTA::CAB::Server::XmlRpc::Procedure->new($listproc) ); | ||
| 176 | $srv->vlog($srv->{logRegisterProc},"registered XML-RPC listing procedure $listproc->{name}()\n"); | ||
| 177 | |||
| 178 | ##-- propagate security and logging options to underlying server | ||
| 179 | $xsrv->{$_} = $srv->{$_} foreach (qw(allowUserOptions logCall logCallData)); | ||
| 180 | |||
| 181 | return 1; | ||
| 182 | } | ||
| 183 | |||
| 184 | ## $rc = $srv->run() | ||
| 185 | ## + run the server | ||
| 186 | sub run { | ||
| 187 | my $srv = shift; | ||
| 188 | $srv->prepare() if (!$srv->{xsrv}); ##-- sanity check | ||
| 189 | $srv->logcroak("run(): no underlying RPC::XML object!") if (!$srv->{xsrv}); | ||
| 190 | $srv->info("server starting on host ", $srv->{xsrv}->host, ", port ", $srv->{xsrv}->port, "\n"); | ||
| 191 | $srv->{xsrv}->server_loop(%{$srv->{runopt}}); | ||
| 192 | $srv->info("server exiting\n"); | ||
| 193 | return $srv->finish(); | ||
| 194 | } | ||
| 195 | |||
| 196 | ##============================================================================== | ||
| 197 | ## Methods: Additional | ||
| 198 | ##============================================================================== | ||
| 199 | |||
| 200 | ## \%procSpec = $srv->listAnalyzersProc() | ||
| 201 | sub listAnalyzersProc { | ||
| 202 | my $srv = shift; | ||
| 203 | my $anames = DTA::CAB::Utils::deep_encode($srv->{encoding}, | ||
| 204 | [ map {($srv->{procNamePrefix}||'').$_ } keys(%{$srv->{as}}) ] | ||
| 205 | ); | ||
| 206 | return { | ||
| 207 | name => ($srv->{procNamePrefix}||'').'listAnalyzers', | ||
| 208 | code => sub { return $anames; }, | ||
| 209 | help => 'list registered analyzer names', | ||
| 210 | signature => [ 'array' ], | ||
| 211 | }; | ||
| 212 | } | ||
| 213 | |||
| 214 | ##======================================================================== | ||
| 215 | ## PACKAGE: DTA::CAB::Server::XmlRpc::Procedure | ||
| 216 | ## + subclass of RPC::XML::Procedure | ||
| 217 | package DTA::CAB::Server::XmlRpc::Procedure; | ||
| 218 | use RPC::XML::Procedure; | ||
| 219 | use strict; | ||
| 220 | use Data::Dumper; | ||
| 221 | our @ISA = ('RPC::XML::Procedure','DTA::CAB::Logger'); | ||
| 222 | |||
| 223 | ## $proc = CLASS->new(\%methodHash) | ||
| 224 | |||
| 225 | ## $rv = $proc->call($XML_RPC_SERVER, @PARAMLIST) | ||
| 226 | sub call { | ||
| 227 | if (defined($_[1]{logCall})) { | ||
| 228 | $_[0]->vlog($_[1]{logCall}, "$_[0]{name}(): client=".($_[1]{peerhost}||'(unavailable)')); #:$_[1]{peerport} | ||
| 229 | } | ||
| 230 | if (defined($_[1]{logCallData})) { | ||
| 231 | local $Data::Dumper::Purity = 1; | ||
| 232 | local $Data::Dumper::Pad = "\t"; | ||
| 233 | local $Data::Dumper::Terse = 0; | ||
| 234 | local $Data::Dumper::Indent = 1; | ||
| 235 | $_[0]->vlog($_[1]{logCallData}, "call:\n", Data::Dumper->Dump([ $_[1]{peerhost}, $_[0]{name}, [@_[2..$#_]]], [qw(CLIENT PROC PARAMS)])); | ||
| 236 | } | ||
| 237 | if (@_ > 3) { | ||
| 238 | return $_[0]->SUPER::call(@_[1..($#_-1)], | ||
| 239 | bless({ | ||
| 240 | ($_[0]{opts} ? (%{$_[0]{opts}}) : qw()), | ||
| 241 | ($_[1]{allowUserOptions} && $_[$#_] ? (%{$_[$#_]}) : qw()), | ||
| 242 | },'RPC::XML::struct'), | ||
| 243 | ); | ||
| 244 | } | ||
| 245 | elsif ($_[0]{opts}) { | ||
| 246 | return $_[0]->SUPER::call(@_[1..$#_], | ||
| 247 | bless( { %{$_[0]{opts}} },'RPC::XML::struct'), | ||
| 248 | ); | ||
| 249 | } | ||
| 250 | else { | ||
| 251 | return $_[0]->SUPER::call(@_[1..$#_]); | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | 1; ##-- be happy | ||
| 258 | |||
| 259 | __END__ |