/usr/share/otrs/Kernel/System/Registration.pm is in otrs2 3.3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 | # --
# Kernel/System/Registration.pm - All Registration functions
# Copyright (C) 2001-2014 OTRS AG, http://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --
package Kernel::System::Registration;
use strict;
use warnings;
use Kernel::System::CacheInternal;
use Kernel::System::Environment;
use Kernel::System::JSON;
use Kernel::System::Scheduler::TaskManager;
use Kernel::System::SystemData;
use Kernel::System::WebUserAgent;
=head1 NAME
Kernel::System::Registration - Registration lib
=head1 SYNOPSIS
All Registration functions.
The Registration API contains calls needed to communicate with the OTRS Group Portal.
The steps to register are:
- Validate OTRS-ID (this results in a token)
- Register the system - this requires the token.
This assures that all registered systems are registered against an existing OTRS-ID.
After registration a registration key is stored in the OTRS System. This key is,
along with system attributes such as OTRS version and Perl version, sent to OTRS in a
weekly update. This ensures the OTRS Group Portal contains up-to-date information on
the current state of the OTRS System.
In order to make sure that registration keys are not used on multiple systems -
something that can happen quite easily when copying a database to a different system -
every update will retrieve a new UpdateID from the OTRS Group Portal. This is used
when communicating the next update; if the received update would not contain the correct
UpdateID the Portal refuses the update and an updated registration is required.
=head1 PUBLIC INTERFACE
=over 4
=cut
=item new()
create an object
use Kernel::Config;
use Kernel::System::Encode;
use Kernel::System::Log;
use Kernel::System::Main;
use Kernel::System::DB;
use Kernel::System::Registration;
my $ConfigObject = Kernel::Config->new();
my $EncodeObject = Kernel::System::Encode->new(
ConfigObject => $ConfigObject,
);
my $LogObject = Kernel::System::Log->new(
ConfigObject => $ConfigObject,
EncodeObject => $EncodeObject,
);
my $MainObject = Kernel::System::Main->new(
ConfigObject => $ConfigObject,
EncodeObject => $EncodeObject,
LogObject => $LogObject,
);
my $DBObject = Kernel::System::DB->new(
ConfigObject => $ConfigObject,
EncodeObject => $EncodeObject,
LogObject => $LogObject,
MainObject => $MainObject,
);
my $RegistrationObject = Kernel::System::Registration->new(
ConfigObject => $ConfigObject,
LogObject => $LogObject,
DBObject => $DBObject,
MainObject => $MainObject,
EncodeObject => $EncodeObject,
);
=cut
sub new {
my ( $Type, %Param ) = @_;
# allocate new hash for object
my $Self = {};
bless( $Self, $Type );
# check needed objects
for (qw(DBObject ConfigObject LogObject MainObject EncodeObject TimeObject)) {
$Self->{$_} = $Param{$_} || die "Got no $_!";
}
# create additional objects
$Self->{EnvironmentObject} = Kernel::System::Environment->new( %{$Self} );
$Self->{JSONObject} = Kernel::System::JSON->new( %{$Self} );
$Self->{SystemDataObject} = Kernel::System::SystemData->new( %{$Self} );
$Self->{TaskObject} = Kernel::System::Scheduler::TaskManager->new( %{$Self} );
$Self->{CacheInternalObject} = Kernel::System::CacheInternal->new(
%{$Self},
Type => 'Registration',
TTL => 60 * 60 * 24 * 20,
);
$Self->{RegistrationURL} = 'https://cloud.otrs.com/otrs/public.pl';
$Self->{APIVersion} = 2;
return $Self;
}
=item TokenGet()
Get a token needed for system registration.
To obtain this token, you need to pass a valid OTRS ID and password.
my %Result = $RegistrationObject->TokenGet(
OTRSID => 'myname@example.com',
Password => 'mysecretpass',
);
returns:
%Result = (
Success => 1,
Token => 'ase1zfa234asfd234afsd1243',
);
or, on failure:
%Result = (
Success => 0,
Reason => "Can't connect to server",
);
or
%Result = (
Success => 0,
Reason => "Username unknown or password incorrect.",
);
=cut
sub TokenGet {
my ( $Self, %Param ) = @_;
# check needed parameters
for (qw(OTRSID Password)) {
if ( !$Param{$_} ) {
$Self->{LogObject}->Log( Priority => 'error', Message => "Need $_!" );
return;
}
}
# create webuseragent object
my $WebUserAgentObject = Kernel::System::WebUserAgent->new(
DBObject => $Self->{DBObject},
ConfigObject => $Self->{ConfigObject},
LogObject => $Self->{LogObject},
MainObject => $Self->{MainObject},
Timeout => 10,
);
# get token
my %Response = $WebUserAgentObject->Request(
Type => 'POST',
URL => $Self->{RegistrationURL},
Data => {
Action => 'PublicRegistration',
Subaction => 'TokenGet',
APIVersion => $Self->{APIVersion},
OTRSID => $Param{OTRSID},
Password => $Param{Password},
},
);
# initiate result hash
my %Result = (
Success => 0,
);
# test if the web response was successful
if ( $Response{Status} ne '200 OK' ) {
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "Registration - Can't contact server - $Response{Status}",
);
$Result{Reason} = "Can't contact registration server. Please try again later.";
return %Result;
}
# make sure we have content as a scalar ref
if ( !$Response{Content} || ref $Response{Content} ne 'SCALAR' ) {
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "Registration - No content received from server",
);
$Result{Reason} = 'No content received from registration server. Please try again later.';
return %Result;
}
# decode JSON data
my $ResponseData = $Self->{JSONObject}->Decode(
Data => ${ $Response{Content} },
);
if ( !$ResponseData || ref $ResponseData ne 'HASH' ) {
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Registration - Can't decode JSON",
);
$Result{Reason} = 'Problems processing server result. Please try again later.';
return %Result;
}
# if auth is incorrect
if ( !defined $ResponseData->{Auth} || $ResponseData->{Auth} ne 'ok' ) {
$Result{Reason} = 'Username and password do not match. Please try again.';
return %Result;
}
# check if token exists in data
if ( !$ResponseData->{Token} ) {
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Registration - received no Token!",
);
$Result{Reason} = 'Problems processing server result. Please try again later.';
return %Result;
}
# return success and token
$Result{Token} = $ResponseData->{Token};
$Result{Success} = 1;
return %Result;
}
=item Register()
Register the system;
my $Success = $RegistrationObject->Register(
Token => '8a85ad4c-e5ff-4b91-a4b3-0b9ea8e2a3dc'
OTRSID => 'myname@example.com'
Type => 'production',
Description => 'Main ticketing system', # optional
);
=cut
sub Register {
my ( $Self, %Param ) = @_;
# check needed parameters
for (qw(Token OTRSID Type)) {
if ( !$Param{$_} ) {
$Self->{LogObject}->Log( Priority => 'error', Message => "Need $_!" );
return;
}
}
# create webuseragent object
my $WebUserAgentObject = Kernel::System::WebUserAgent->new(
DBObject => $Self->{DBObject},
ConfigObject => $Self->{ConfigObject},
LogObject => $Self->{LogObject},
MainObject => $Self->{MainObject},
Timeout => 10,
);
# load operating system info from environment object
my %OSInfo = $Self->{EnvironmentObject}->OSInfoGet();
my %System = (
PerlVersion => sprintf( "%vd", $^V ),
OSType => $OSInfo{OS},
OSVersion => $OSInfo{OSName},
OTRSVersion => $Self->{ConfigObject}->Get('Version'),
FQDN => $Self->{ConfigObject}->Get('FQDN'),
DatabaseVersion => $Self->{DBObject}->Version(),
);
# load old registration data if we have this
my %OldRegistration = $Self->RegistrationDataGet();
my %Response = $WebUserAgentObject->Request(
Type => 'POST',
URL => $Self->{RegistrationURL},
Data => {
%System,
Action => 'PublicRegistration',
Subaction => 'Register',
APIVersion => $Self->{APIVersion},
State => 'active',
OldUniqueID => $OldRegistration{UniqueID} || '',
OldAPIKey => $OldRegistration{APIKey} || '',
Token => $Param{Token},
OTRSID => $Param{OTRSID},
Type => $Param{Type},
Description => $Param{Description},
},
);
# test if the web response was successful
if ( $Response{Status} ne '200 OK' ) {
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "Registration - Can't contact server - $Response{Status}",
);
return;
}
if ( !$Response{Content} || ref $Response{Content} ne 'SCALAR' ) {
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "Registration - No content received from server",
);
return;
}
# decode JSON data
my $ResponseData = $Self->{JSONObject}->Decode(
Data => ${ $Response{Content} },
);
if ( !$ResponseData || ref $ResponseData ne 'HASH' ) {
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Registration - Can't decode JSON",
);
return;
}
# check if data exists
for my $Key (qw(UniqueID APIKey LastUpdateID NextUpdate)) {
if ( !$ResponseData->{$Key} ) {
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Registration - received no $Key!: " . ${ $Response{Content} },
);
return;
}
}
# log response, add data to system
$Self->{LogObject}->Log(
Priority => 'info',
Message => "Registration - received UniqueID '$ResponseData->{UniqueID}'.",
);
my %RegistrationData = (
State => 'registered',
UniqueID => $ResponseData->{UniqueID},
APIKey => $ResponseData->{APIKey},
LastUpdateID => $ResponseData->{LastUpdateID},
LastUpdateTime => $Self->{TimeObject}->CurrentTimestamp(),
);
# only add keys if the system has never been registered before
# otherwise we should store the original unique ID for future reference
# so we can keep an overview of all previously registered unique IDs
if ( !$OldRegistration{UniqueID} ) {
for my $Key (qw(State UniqueID APIKey LastUpdateID LastUpdateTime Description Type)) {
$Self->{SystemDataObject}->SystemDataAdd(
Key => 'Registration::' . $Key,
Value => $RegistrationData{$Key} || '',
UserID => 1,
);
}
}
else {
# store original UniqueID, but only if we get back a new one
if ( $OldRegistration{UniqueID} ne $RegistrationData{UniqueID} ) {
$Self->{SystemDataObject}->SystemDataAdd(
Key => 'RegistrationUniqueIDs::' . $OldRegistration{UniqueID},
Value => $OldRegistration{UniqueID},
UserID => 1,
);
}
# update registration information
for my $Key (qw(State UniqueID APIKey LastUpdateID LastUpdateTime Description Type)) {
$Self->{SystemDataObject}->SystemDataUpdate(
Key => 'Registration::' . $Key,
Value => $RegistrationData{$Key} || '',
UserID => 1,
);
}
}
# calculate due date for next update, fall back to 24h
my $NextUpdateSeconds = int $ResponseData->{NextUpdate} || ( 3600 * 24 );
my $DueTime = $Self->{TimeObject}->SystemTime2TimeStamp(
SystemTime => $Self->{TimeObject}->SystemTime() + $NextUpdateSeconds,
);
# schedule update in scheduler
# after first update the updates will reschedule itself
my $Result = $Self->{TaskObject}->TaskAdd(
Type => 'RegistrationUpdate',
DueTime => $DueTime,
Data => {
ReSchedule => 1,
},
);
return 1;
}
=item RegistrationDataGet()
Get the registration data from the system.
my %RegistrationInfo = $RegistrationObject->RegistrationDataGet();
=cut
sub RegistrationDataGet {
my ( $Self, %Param ) = @_;
my %RegistrationData = $Self->{SystemDataObject}->SystemDataGroupGet(
Group => 'Registration',
UserID => 1,
);
# return empty hash if no UniqueID is found
return () if !$RegistrationData{UniqueID};
return %RegistrationData;
}
=item RegistrationUpdateSend()
Register the system as Active.
This also updates any information on Database, OTRS Version and Perl version that
might have changed.
If you provide Type and Description, these will be sent to the registration server.
my %Result = $RegistrationObject->RegistrationUpdateSend();
my %Result = $RegistrationObject->RegistrationUpdateSend(
Type => 'test',
Description => 'new test system',
);
returns
%Result = (
Success => 1,
ReScheduleIn => 604800, # number of seconds for next update
);
or
%Result = (
Success => 0,
Reason => 'Could not reach server', # or other
);
=cut
sub RegistrationUpdateSend {
my ( $Self, %Param ) = @_;
# get registration data
my %RegistrationData = $Self->RegistrationDataGet();
# create webuseragent object
my $WebUserAgentObject = Kernel::System::WebUserAgent->new(
DBObject => $Self->{DBObject},
ConfigObject => $Self->{ConfigObject},
LogObject => $Self->{LogObject},
MainObject => $Self->{MainObject},
Timeout => 10,
);
# read data from environment object
my %OSInfo = $Self->{EnvironmentObject}->OSInfoGet();
my %System = (
PerlVersion => sprintf( "%vd", $^V ),
OSType => $OSInfo{OS},
OSVersion => $OSInfo{OSName},
OTRSVersion => $Self->{ConfigObject}->Get('Version'),
FQDN => $Self->{ConfigObject}->Get('FQDN'),
DatabaseVersion => $Self->{DBObject}->Version(),
);
# add description and type if they are set
KEY:
for my $Key (qw(Type Description)) {
next KEY if !defined $Param{$Key};
$System{$Key} = $Param{$Key};
}
# define result
my %Result = (
Success => 0,
);
my %Response = $WebUserAgentObject->Request(
Type => 'POST',
URL => $Self->{RegistrationURL},
Data => {
%System,
Action => 'PublicRegistration',
Subaction => 'Update',
APIVersion => $Self->{APIVersion},
State => 'active',
APIKey => $RegistrationData{APIKey},
LastUpdateID => $RegistrationData{LastUpdateID},
RegistrationKey => $RegistrationData{UniqueID},
},
);
# test if the web response was successful
if ( $Response{Status} ne '200 OK' ) {
$Result{Reason} = "Can't connect to server - $Response{Status}";
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "RegistrationUpdate - $Result{Reason}",
);
return %Result;
}
# check if we have content as a scalar ref
if ( !$Response{Content} || ref $Response{Content} ne 'SCALAR' ) {
$Result{Reason} = 'No content received from registration server. Please try again later.';
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "RegistrationUpdate - $Result{Reason}",
);
return %Result;
}
# convert internal used charset
$Self->{EncodeObject}->EncodeInput( $Response{Content} );
# decode JSON data
my $ResponseData = $Self->{JSONObject}->Decode(
Data => ${ $Response{Content} },
);
if ( !$ResponseData || ref $ResponseData ne 'HASH' ) {
$Result{Reason} = "Can't decode JSON: '" . ${ $Response{Content} } . "'!";
$Self->{LogObject}->Log(
Priority => 'error',
Message => "RegistrationUpdate - $Result{Reason}",
);
return %Result;
}
# check if data exists
ATTRIBUTE:
for my $Attribute (qw(UpdateID Type Description)) {
next ATTRIBUTE if defined $ResponseData->{$Attribute};
$Result{Reason} = "Received no '$Attribute': '" . ${ $Response{Content} } . "'!";
$Self->{LogObject}->Log(
Priority => 'error',
Message => "RegistrationUpdate - $Result{Reason}!",
);
return %Result;
}
# log response, write data.
$Self->{LogObject}->Log(
Priority => 'info',
Message => "RegistrationUpdate - received UpdateID '$ResponseData->{UpdateID}'.",
);
# gather and update provided data in SystemData table
my %UpdateData = (
LastUpdateID => $ResponseData->{UpdateID},
LastUpdateTime => $Self->{TimeObject}->CurrentTimestamp(),
Type => $ResponseData->{Type},
Description => $ResponseData->{Description},
);
# if the registration server provided a new UniqueID and API key, use those.
if ( $ResponseData->{UniqueID} && $ResponseData->{APIKey} ) {
# add data to Update hash
$UpdateData{UniqueID} = $ResponseData->{UniqueID};
$UpdateData{APIKey} = $ResponseData->{APIKey};
# preserve old UniqueID
$Self->{SystemDataObject}->SystemDataAdd(
Key => 'RegistrationUniqueIDs::' . $RegistrationData{UniqueID},
Value => $RegistrationData{UniqueID},
UserID => 1,
);
}
for my $Key ( sort keys %UpdateData ) {
$Self->{SystemDataObject}->SystemDataUpdate(
Key => 'Registration::' . $Key,
Value => $UpdateData{$Key},
UserID => 1,
);
}
$Result{Success} = 1;
$Result{ReScheduleIn} = $ResponseData->{NextUpdate} // ( 3600 * 7 * 24 );
return %Result;
}
=item Deregister()
Deregister the system. Deregistering also stops any update jobs.
my $Success = $RegistrationObject->Deregister(
Token => '8a85ad4c-e5ff-4b91-a4b3-0b9ea8e2a3dc',
OTRSID => 'myname@example.com',
);
returns '1' for success or a description if there was no success
=cut
sub Deregister {
my ( $Self, %Param ) = @_;
# check needed parameters
for (qw(Token OTRSID)) {
if ( !$Param{$_} ) {
$Self->{LogObject}->Log( Priority => 'error', Message => "Need $_!" );
return;
}
}
# create webuseragent object
my $WebUserAgentObject = Kernel::System::WebUserAgent->new(
DBObject => $Self->{DBObject},
ConfigObject => $Self->{ConfigObject},
LogObject => $Self->{LogObject},
MainObject => $Self->{MainObject},
Timeout => 10,
);
my %RegistrationInfo = $Self->RegistrationDataGet();
my %Response = $WebUserAgentObject->Request(
Type => 'POST',
URL => $Self->{RegistrationURL},
Data => {
Action => 'PublicRegistration',
Subaction => 'Deregister',
APIVersion => $Self->{APIVersion},
OTRSID => $Param{OTRSID},
Token => $Param{Token},
APIKey => $RegistrationInfo{APIKey},
RegistrationKey => $RegistrationInfo{UniqueID},
},
);
# test if the web response was successful
if ( $Response{Status} ne '200 OK' ) {
my $Result = "Can't contact server - $Response{Status}";
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "Registration - $Result",
);
return $Result;
}
if ( !$Response{Content} || ref $Response{Content} ne 'SCALAR' ) {
my $Result = 'No content received from server';
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "Registration - $Result",
);
return $Result;
}
# decode JSON data
my $ResponseData = $Self->{JSONObject}->Decode(
Data => ${ $Response{Content} },
);
if ( !$ResponseData || ref $ResponseData ne 'HASH' ) {
my $Result = 'Can\'t decode JSON';
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Deregistration - $Result",
);
return $Result;
}
# check success
if ( !$ResponseData->{Success} ) {
my $Result = "Received no response";
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Deregistration $Result " . ${ $Response{Content} },
);
return $Result;
}
# log response, add data to system
$Self->{LogObject}->Log(
Priority => 'info',
Message => "Registration - deregistered '$RegistrationInfo{UniqueID}'.",
);
$Self->{SystemDataObject}->SystemDataUpdate(
Key => 'Registration::State',
Value => 'deregistered',
UserID => 1,
);
# remove RegistrationUpdate scheduler task
my @TaskList = $Self->{TaskObject}->TaskList();
TASK:
for my $Task (@TaskList) {
next TASK if $Task->{Type} ne 'RegistrationUpdate';
$Self->{TaskObject}->TaskDelete( ID => $Task->{ID} );
}
return 1;
}
1;
=back
=head1 TERMS AND CONDITIONS
This software is part of the OTRS project (L<http://otrs.org/>).
This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (AGPL). If you
did not receive this file, see L<http://www.gnu.org/licenses/agpl.txt>.
=cut
|