| 1 | package ZimbraManager::SOAP; | ||
| 2 | |||
| 3 | use Mojo::Base -base; | ||
| 4 | |||
| 5 | |||
| 6 | use Mojo::Util qw(dumper); | ||
| 7 | use Mojo::Log; | ||
| 8 | |||
| 9 | use IO::Socket::SSL qw( SSL_VERIFY_NONE SSL_VERIFY_PEER ); | ||
| 10 | |||
| 11 | use XML::Compile::WSDL11; | ||
| 12 | use XML::Compile::SOAP11; | ||
| 13 | use XML::Compile::SOAP::Trace; | ||
| 14 | use XML::Compile::Transport::SOAPHTTP; | ||
| 15 | |||
| 16 | use HTTP::CookieJar::LWP; | ||
| 17 | |||
| 18 | |||
| 19 | has 'log' => sub { | ||
| 20 | return Mojo::Log->new(); | ||
| 21 | }; | ||
| 22 | |||
| 23 | |||
| 24 | has 'soapDebug' => sub { | ||
| 25 | my $self = shift; | ||
| 26 | return 0; | ||
| 27 | }; | ||
| 28 | |||
| 29 | |||
| 30 | has 'soapErrorsToConsumer' => sub { | ||
| 31 | my $self = shift; | ||
| 32 | return 0; | ||
| 33 | }; | ||
| 34 | |||
| 35 | |||
| 36 | has 'mode' => sub { | ||
| 37 | my $self = shift; | ||
| 38 | return 'full'; | ||
| 39 | }; | ||
| 40 | |||
| 41 | |||
| 42 | has 'wsdlPath' => sub { | ||
| 43 | my $self = shift; | ||
| 44 | return "$FindBin::Bin/../etc/wsdl/"; | ||
| 45 | }; | ||
| 46 | |||
| 47 | |||
| 48 | has 'wsdlFile' => sub { | ||
| 49 | my $self = shift; | ||
| 50 | my $wsdlFile; | ||
| 51 | my $mode = $self->mode; | ||
| 52 | if ($mode eq 'admin') { | ||
| 53 | $wsdlFile = $self->wsdlPath.'ZimbraAdminService.wsdl'; | ||
| 54 | } | ||
| 55 | elsif ($mode eq 'user') { | ||
| 56 | $wsdlFile = $self->wsdlPath.'ZimbraUserService.wsdl'; | ||
| 57 | } | ||
| 58 | elsif ($mode eq 'full') { | ||
| 59 | $wsdlFile = $self->wsdlPath.'ZimbraService.wsdl'; | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | die "no valid mode ($self->mode) for attribute wsdlFile has been set"; | ||
| 63 | } | ||
| 64 | $self->log->debug("wsdlFile=$wsdlFile"); | ||
| 65 | return $wsdlFile; | ||
| 66 | }; | ||
| 67 | |||
| 68 | |||
| 69 | has 'wsdlXml' => sub { | ||
| 70 | my $self = shift; | ||
| 71 | my $wsdlXml = XML::LibXML->new->parse_file($self->wsdlFile); | ||
| 72 | return $wsdlXml; | ||
| 73 | }; | ||
| 74 | |||
| 75 | |||
| 76 | has 'wsdl' => sub { | ||
| 77 | my $self = shift; | ||
| 78 | my $wsdlXml = $self->wsdlXml; | ||
| 79 | my $wsdl = XML::Compile::WSDL11->new($wsdlXml); | ||
| 80 | for my $xsd (glob $self->wsdlPath."*.xsd") { | ||
| 81 | $self->log->debug("XML Schema Import of file:", $xsd); | ||
| 82 | $wsdl->importDefinitions($xsd); | ||
| 83 | } | ||
| 84 | return $wsdl; | ||
| 85 | }; | ||
| 86 | |||
| 87 | |||
| 88 | has 'zcsService' => sub { | ||
| 89 | my $self = shift; | ||
| 90 | my $mode = $self->mode; | ||
| 91 | if ($mode eq 'admin') { | ||
| 92 | return 'zcsAdminService'; | ||
| 93 | } | ||
| 94 | elsif ($mode eq 'user') { | ||
| 95 | return 'zcsService'; | ||
| 96 | } | ||
| 97 | elsif ($mode eq 'full') { | ||
| 98 | return 'zcsAdminService'; | ||
| 99 | } | ||
| 100 | else { | ||
| 101 | die "no valid mode ($self->mode) for attribute zcsService has been set"; | ||
| 102 | } | ||
| 103 | }; | ||
| 104 | |||
| 105 | |||
| 106 | has 'wsdlReturnParameterName' => sub { | ||
| 107 | return 'params'; | ||
| 108 | }; | ||
| 109 | |||
| 110 | |||
| 111 | has 'service' => sub { | ||
| 112 | my $self = shift; | ||
| 113 | my $wsdlXml = $self->wsdlXml; | ||
| 114 | my $zimbraServices; | ||
| 115 | my $wsdlServices = $wsdlXml->getElementsByTagName( 'wsdl:service' ); | ||
| 116 | for my $service (@$wsdlServices) { | ||
| 117 | my $name = $service->getAttribute( 'name' ); | ||
| 118 | my $port = $service->getElementsByTagName( 'wsdl:port' )->[0]; | ||
| 119 | my $port_name = $port->getAttribute( 'name' ); | ||
| 120 | my $address = $port->getElementsByTagName( 'soap:address' )->[0]; | ||
| 121 | my $uri = $address->getAttribute( 'location' ); | ||
| 122 | $uri =~ m/^(https|http):\/\/(.+?):(.+?)\//; | ||
| 123 | my $uri_protocol = $1; | ||
| 124 | my $uri_host = $2; | ||
| 125 | my $uri_port = $3; | ||
| 126 | |||
| 127 | $zimbraServices->{$name} = { | ||
| 128 | host => $uri_host, | ||
| 129 | name => $name, | ||
| 130 | port_name => $port_name, | ||
| 131 | uri => $uri, | ||
| 132 | uri_host => $uri_host, | ||
| 133 | uri_port => $uri_port, | ||
| 134 | uri_protocol => $uri_protocol, | ||
| 135 | }; | ||
| 136 | } | ||
| 137 | if ($self->log->level eq 'debug') { | ||
| 138 | for my $servicename (keys %$zimbraServices) { | ||
| 139 | for my $k (keys %{$zimbraServices->{$servicename}}) { | ||
| 140 | $self->log->debug("$k=", $zimbraServices->{$servicename}->{$k}); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | return $zimbraServices->{$self->zcsService}; | ||
| 145 | }; | ||
| 146 | |||
| 147 | |||
| 148 | has 'transporter' => sub { | ||
| 149 | my $self = shift; | ||
| 150 | |||
| 151 | my $uri = $self->service->{uri}; | ||
| 152 | |||
| 153 | my $verifyHostname = $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} // 1; | ||
| 154 | my $verifyMode = $ENV{PERL_LWP_SSL_VERIFY_MODE} // SSL_VERIFY_PEER; | ||
| 155 | |||
| 156 | # redirect the endpoint as specified in the WSDL to our own server. | ||
| 157 | my $transporter = XML::Compile::Transport::SOAPHTTP->new( | ||
| 158 | address => $uri, | ||
| 159 | keep_alive => 1, | ||
| 160 | # for SSL handling we need our own LWP Agent | ||
| 161 | user_agent => LWP::UserAgent->new( | ||
| 162 | ssl_opts => { # default is SSL verification on | ||
| 163 | # NOTE: PERL_LWP_SSL_VERIFY_MODE is not an "official" env variable | ||
| 164 | verify_hostname => $verifyHostname, | ||
| 165 | SSL_verify_mode => $verifyMode, | ||
| 166 | }, | ||
| 167 | ), | ||
| 168 | ); | ||
| 169 | |||
| 170 | $self->log->debug("LWP_SSL_VERIFY_HOSTNAME = $verifyHostname\n" | ||
| 171 | . "LWP_SSL_VERIFY_MODE = $verifyMode"); | ||
| 172 | |||
| 173 | return $transporter; | ||
| 174 | }; | ||
| 175 | |||
| 176 | |||
| 177 | has 'soapOps' => sub { | ||
| 178 | my $self = shift; | ||
| 179 | my $soapOps; | ||
| 180 | |||
| 181 | my $port = $self->service->{port_name}; | ||
| 182 | my $name = $self->service->{name}; | ||
| 183 | my $send = $self->transporter->compileClient( port => $port ); | ||
| 184 | |||
| 185 | # Compile all service methods | ||
| 186 | for my $soapOp ( $self->wsdl->operations( port => $port ) ) { | ||
| 187 | $self->log->debug("Got soap operation " . $soapOp->name); | ||
| 188 | $soapOps->{ $soapOp->name } = | ||
| 189 | $self->wsdl->compileClient( | ||
| 190 | $soapOp->name, | ||
| 191 | port => $port, | ||
| 192 | service => $name, | ||
| 193 | transport => $send, | ||
| 194 | ); | ||
| 195 | } | ||
| 196 | return $soapOps; | ||
| 197 | }; | ||
| 198 | |||
| 199 | |||
| 200 | sub callLegacy { | ||
| 201 | my $self = shift; | ||
| 202 | my $action = shift; | ||
| 203 | my $args = shift; | ||
| 204 | my $authToken = shift; | ||
| 205 | my $namedParameters = { | ||
| 206 | action => $action, | ||
| 207 | args => $args, | ||
| 208 | authToken => $authToken, | ||
| 209 | }; | ||
| 210 | return $self->call($namedParameters); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub call { | ||
| 214 | my $self = shift; | ||
| 215 | my $namedParameters = shift; | ||
| 216 | if (ref $namedParmeters ne 'HASH') { | ||
| 217 | $namedParameters = { @_ }; | ||
| 218 | } | ||
| 219 | my $action = $namedParameters->{action}; | ||
| 220 | my $args = $namedParameters->{args}; | ||
| 221 | my $authToken = $namedParameters->{authToken}; | ||
| 222 | |||
| 223 | my $uri = $self->service->{uri}; | ||
| 224 | |||
| 225 | # for each controller request build authentication token with cookieJar | ||
| 226 | # HTTP::CookieJar::LWP is compatible with the original LWP cookie | ||
| 227 | # mechanism but lightweight | ||
| 228 | |||
| 229 | my $cookieJar = HTTP::CookieJar::LWP->new(); | ||
| 230 | $cookieJar->add( $uri, "ZM_ADMIN_AUTH_TOKEN=$authToken" ) if (defined $authToken); | ||
| 231 | my $ua = $self->transporter->userAgent(); | ||
| 232 | $ua->cookie_jar($cookieJar); | ||
| 233 | |||
| 234 | $self->log->debug(dumper({ | ||
| 235 | _function => 'ZimbraManager::Soap::call', | ||
| 236 | action =>$action, | ||
| 237 | args =>$args, | ||
| 238 | authToken =>$authToken | ||
| 239 | })); | ||
| 240 | |||
| 241 | my ( $response, $trace ) = $self->soapOps->{$action}->($args); | ||
| 242 | if ($self->soapDebug) { | ||
| 243 | $self->log->debug(dumper ("call(): response=", $response)); | ||
| 244 | $self->log->debug(dumper ("call(): trace=", $trace)); | ||
| 245 | } | ||
| 246 | my $err; | ||
| 247 | if ( not defined $response ) { | ||
| 248 | $err = 'SOAP ERROR from Zimbra: undefined response'; | ||
| 249 | if ($self->soapErrorsToConsumer) { | ||
| 250 | my $trace = 'trace: ' . dumper($trace); | ||
| 251 | $err .= "\n\n\n$trace\n"; | ||
| 252 | } | ||
| 253 | } | ||
| 254 | elsif ( $response->{Fault} ) { | ||
| 255 | $err = 'SOAP ERROR from Zimbra: '. $response->{Fault}->{faultstring}; | ||
| 256 | if ($self->soapErrorsToConsumer) { | ||
| 257 | my $response = 'response: ' . dumper($response); | ||
| 258 | my $trace = 'trace: ' . dumper($trace); | ||
| 259 | $err .= "\n\n\n$response\n\n$trace\n"; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | return ($response->{$self->wsdlReturnParameterName}, $err); | ||
| 263 | } | ||
| 264 | |||
| 265 | 1; | ||
| 266 | |||
| 267 | =pod | ||
| 268 | |||
| 269 | =encoding UTF-8 | ||
| 270 | |||
| 271 | =head1 NAME | ||
| 272 | |||
| 273 | ZimbraManager::SOAP | ||
| 274 | |||
| 275 | =head1 VERSION | ||
| 276 | |||
| 277 | version 0.15 | ||
| 278 | |||
| 279 | =head1 SYNOPSIS | ||
| 280 | |||
| 281 | use ZimbraManager::SOAP; | ||
| 282 | |||
| 283 | has 'soap' => sub { | ||
| 284 | my $self = shift; | ||
| 285 | return ZimbraManager::SOAP->new( | ||
| 286 | log => $self->log, | ||
| 287 | mode => 'full' # or 'admin' or 'user' | ||
| 288 | # soapDebug => '1', # enables SOAP backend communication debugging | ||
| 289 | # soapErrorsToConsumer => '1' # returns SOAP error to consumer | ||
| 290 | |||
| 291 | ); | ||
| 292 | }; | ||
| 293 | |||
| 294 | my $namedParameters = { | ||
| 295 | action => 'FUNCTIONNAME', | ||
| 296 | args => \%DATASTRUCTUREDPARAMS, | ||
| 297 | authToken => $authToken, | ||
| 298 | }; | ||
| 299 | my ($ret, $err) = $self->soap->call($namedParameters); | ||
| 300 | |||
| 301 | also | ||
| 302 | $self->soap->call( | ||
| 303 | action => 'FUNCTIONNAME', | ||
| 304 | args => \%DATASTRUCTUREDPARAMS, | ||
| 305 | authToken => $authToken, | ||
| 306 | ); | ||
| 307 | |||
| 308 | is valid | ||
| 309 | |||
| 310 | =head1 DESCRIPTION | ||
| 311 | |||
| 312 | Helper class for Zimbra adminstration interface. | ||
| 313 | |||
| 314 | =head1 NAME | ||
| 315 | |||
| 316 | ZimbraManager::SOAP - class to manage Zimbra with perl and SOAP | ||
| 317 | |||
| 318 | =head1 ATTRIBUTES | ||
| 319 | |||
| 320 | =head2 Logging and Debugging Attributes | ||
| 321 | |||
| 322 | =head3 log | ||
| 323 | |||
| 324 | The mojo log object | ||
| 325 | |||
| 326 | =head3 soapDebug | ||
| 327 | |||
| 328 | Enabled SOAP debug output to $self->log | ||
| 329 | |||
| 330 | =head3 soapErrorsToConsumer | ||
| 331 | |||
| 332 | Returns soapErrors to the consumer in the return messages | ||
| 333 | |||
| 334 | =head2 mode | ||
| 335 | |||
| 336 | The zimbra SOAP interface has three modes: | ||
| 337 | |||
| 338 | admin: adminstration access and commands in admin context | ||
| 339 | user: user accesses and commands in user context | ||
| 340 | full: both admin and user | ||
| 341 | |||
| 342 | =head2 wsdlPath | ||
| 343 | |||
| 344 | Path to WSDL and XML Schema file(s) | ||
| 345 | |||
| 346 | =head2 wsdlFile | ||
| 347 | |||
| 348 | Select WSDL file according to mode | ||
| 349 | |||
| 350 | =head2 wsdlXml | ||
| 351 | |||
| 352 | parsed WSDL as XML | ||
| 353 | |||
| 354 | =head2 wsdl | ||
| 355 | |||
| 356 | parsed WSDL as perl object with corresponding included XML Schema | ||
| 357 | and function stubs. | ||
| 358 | |||
| 359 | =head2 zcsService | ||
| 360 | |||
| 361 | Internal WSDL name for selecting mode | ||
| 362 | |||
| 363 | =head2 wsdlReturnParameterName | ||
| 364 | |||
| 365 | Name of the return value / structure | ||
| 366 | |||
| 367 | In Zimbra 8.0.7 this will be 'params' as defined here: | ||
| 368 | |||
| 369 | <wsdl:definitions> | ||
| 370 | ... | ||
| 371 | <wsdl:message name="AdminAbortHsmRequestMessage"> | ||
| 372 | <wsdl:part name="params" element="zimbraAdmin:AbortHsmRequest"/> | ||
| 373 | </wsdl:message> | ||
| 374 | ... | ||
| 375 | </wsdl:definitions> | ||
| 376 | |||
| 377 | In Zimbra 8.0.6 and previous versions this has been 'parameters' as defined here: | ||
| 378 | |||
| 379 | <wsdl:definitions> | ||
| 380 | ... | ||
| 381 | <wsdl:message name="AdminAbortHsmRequestMessage"> | ||
| 382 | <wsdl:part name="parameters" element="zimbraAdmin:AbortHsmRequest"/> | ||
| 383 | </wsdl:message> | ||
| 384 | ... | ||
| 385 | </wsdl:definitions> | ||
| 386 | |||
| 387 | =head2 service | ||
| 388 | |||
| 389 | Processed SOAP service URI on the server | ||
| 390 | |||
| 391 | =head2 transporter | ||
| 392 | |||
| 393 | Transporter for the SOAP calls - we are using LWP and handle these parameters | ||
| 394 | |||
| 395 | - SSL VERIFY HOSTNAME (see IO::Socket::SSL) | ||
| 396 | - SSL VERIFY MODE (see IO::Socket::SSL) | ||
| 397 | - HTTP keep_alive | ||
| 398 | |||
| 399 | =head2 soapOps | ||
| 400 | |||
| 401 | All usable SOAP operations exported by the the SOAP interface and with the selected | ||
| 402 | mode. | ||
| 403 | |||
| 404 | =head1 METHODS | ||
| 405 | |||
| 406 | All the methods of L<Mojo::Base> plus: | ||
| 407 | |||
| 408 | =head2 call | ||
| 409 | |||
| 410 | Calls Zimbra with the given argument and returns the SOAP response as perl hash. | ||
| 411 | |||
| 412 | =head1 COPYRIGHT | ||
| 413 | |||
| 414 | Copyright (c) 2014 by Roman Plessl. All rights reserved. | ||
| 415 | |||
| 416 | =head1 LICENSE | ||
| 417 | |||
| 418 | This program is free software: you can redistribute it and/or modify it | ||
| 419 | under the terms of the GNU General Public License as published by the Free | ||
| 420 | Software Foundation, either version 3 of the License, or (at your option) | ||
| 421 | any later version. | ||
| 422 | |||
| 423 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 424 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 425 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 426 | more details. | ||
| 427 | |||
| 428 | You should have received a copy of the GNU General Public License along with | ||
| 429 | this program. If not, see L<http://www.gnu.org/licenses/>. | ||
| 430 | |||
| 431 | =head1 AUTHOR | ||
| 432 | |||
| 433 | S<Roman Plessl E<lt>roman@plessl.infoE<gt>> | ||
| 434 | |||
| 435 | =head1 HISTORY | ||
| 436 | |||
| 437 | 2014-03-20 rp Initial Version | ||
| 438 | 2014-03-27 rp Improved Error and SSL Handling | ||
| 439 | 2014-04-29 rp Improved API and Session Handing | ||
| 440 | |||
| 441 | =head1 AUTHOR | ||
| 442 | |||
| 443 | Roman Plessl <rplessl@cpan.org> | ||
| 444 | |||
| 445 | =head1 COPYRIGHT AND LICENSE | ||
| 446 | |||
| 447 | This software is Copyright (c) 2014 by Roman Plessl. | ||
| 448 | |||
| 449 | This is free software, licensed under: | ||
| 450 | |||
| 451 | The GNU General Public License, Version 3, June 2007 | ||
| 452 | |||
| 453 | =cut | ||
| 454 | |||
| 455 | __END__ |