#!/usr/bin/perl
##LICENSE##
package RVL::RVGlobalSoft;
use strict;
use warnings;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use RVL::oAuth;
use Class::Std::Utils;
{
sub new {
my ($class, $opt) = @_;
my (%hash);
$hash{ENV} = \%ENV;
my ($this) = bless( \%hash, $class);
return $this;
}
=help
Returns a singleton Registry instance.
=cut
sub singleton {
my ($class) = __PACKAGE__;
if (!-d RVL::CpHandle::singleton()->getHomeDir() . '/.rvglobalsoft') {
RVL::File::mkdirp(RVL::CpHandle::singleton()->getHomeDir() . '/.rvglobalsoft');
}
if (!defined $RVL::INSTANCE{$class}) {
$RVL::INSTANCE{$class} = $class->new();
}
return $RVL::INSTANCE{$class};
}
sub isHaveAccessKey {
my ($this) = @_;
my $authorizeIdFile = 'authorizeid.pub';
my $isUser = RVL::CpHandle::singleton()->isUser();
if($isUser){
$authorizeIdFile = 'cp_authorizeid.pub';
if(-f RVL::CpHandle::singleton()->getHomeDir() . '/.rvglobalsoft/' . $authorizeIdFile){
system('rm','-f', RVL::CpHandle::singleton()->getHomeDir() . '/.rvglobalsoft/' . $authorizeIdFile);
}
}
if ( -f RVL::CpHandle::singleton()->getHomeDir() . '/.rvglobalsoft/accessid.pub'
|| -f RVL::CpHandle::singleton()->getHomeDir() . '/.rvglobalsoft/' . $authorizeIdFile) {
return 1;
} else {
return 0;
}
}
sub getAccesskey {
my ($this) = @_;
my $errorMsg = '';
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
$this = RVL::RVGlobalSoft::singleton() if (!$this);
# Check perm and redirect if CANNOT usage RV Global Store.
#RVL::RVGlobalSoftPerm::checkOwnerPerm();
my ($authorizeId, $authorizeKey);
my $isUser = RVL::CpHandle::singleton()->isUser();
my ($ownerDir) = RVL::CpHandle::singleton()->getOwnerDir();
my ($username) = RVL::CpHandle::singleton()->getUsername();
my ($hostname) = RVL::CpHandle::singleton()->getHostname();
my $authorizeIdUserFile = 'cp_authorizeid.pub';
if (-f $ownerDir . '/.rvglobalsoft/authorizeid.pub') {
open(FD, '<', $ownerDir . '/.rvglobalsoft/authorizeid.pub') ;
$authorizeId = join('', <FD>);
close(FD);
chomp $authorizeId;
}
my ($cpUserId) = $this->getRVGUserId(
$authorizeId,
$username,
$hostname
);
my $authorizeIdFileUserPath = RVL::CpHandle::singleton()->getHomeDir() . '/.rvglobalsoft/' . $authorizeIdUserFile;
my ($authorizeIdUser);
if($isUser && -f $authorizeIdFileUserPath){
$authorizeIdUser = getUserID($authorizeIdFileUserPath);
if($cpUserId ne $authorizeIdUser)
{
unlink($authorizeIdFileUserPath);
}
}
if (!$this->isHaveAccessKey()) {
RVL::logMessage('IS NOT Have AccessKey', __CONSTANT__::RVL_LOG_DEBUG);
$hostname =~ s/\r|\n//g;
RVL::logMessage($ownerDir, __CONSTANT__::RVL_LOG_DEBUG);
if (-f $ownerDir . '/.rvglobalsoft/authorizeid.pub') {
open(FD, '<', $ownerDir . '/.rvglobalsoft/authorizeid.pub') ;
$authorizeId = join('', <FD>);
close(FD);
chomp $authorizeId;
}
if($authorizeId eq ''){
$errorMsg = 'This system have not configue api keys';
}
RVL::logMessage($ownerDir . '/.rvglobalsoft/accesskey.pem',__CONSTANT__::RVL_LOG_DEBUG);
open(FD, '<', $ownerDir . '/.rvglobalsoft/accesskey.pem');
$authorizeKey = join('', <FD>);
close(FD);
chomp $authorizeKey;
my ($cpUserId) = $this->getRVGUserId(
$authorizeId,
$username,
$hostname
);
my ($oAuth) = RVL::oAuth::singleton({
'uid' => $cpUserId,
'url' => RVL::CpHandle::singleton()->getApiUrl(),
'cp_owner' => $authorizeId,
'cp_hostname' => $hostname,
'cp_username' => $username,
});
my $authorizeIdFile = 'authorizeid.pub';
my $authorizekeyFile = 'authorizekey.pub';
if($isUser){
$authorizeIdFile = 'cp_authorizeid.pub';
$authorizekeyFile = 'cp_authorizekey.pub';
}
my ($apiRes) = $oAuth->request_apikey();
my ($homedir) = RVL::CpHandle::singleton()->getHomeDir();
if (defined $apiRes->{'key'} && defined $apiRes->{'code'}) {
my ($pass) = $oAuth->encryptPublic($authorizeKey, $apiRes->{'key'});
my ($accessKey) = $oAuth->decryptPublickey($apiRes->{'code'}, $pass);
if ($accessKey !~ /-----BEGIN PUBLIC KEY-----(.*?)-----END PUBLIC KEY-----/si) {
$errorMsg = "Have problem to get api key $accessKey";
} else {
chomp($accessKey);
open(FD, '>', $homedir . '/.rvglobalsoft/' . $authorizekeyFile);
print FD $accessKey;
close(FD);
chmod(0644, $homedir . '/.rvglobalsoft/' . $authorizekeyFile);
}
} else {
$errorMsg = "Have problem to get api key";
}
}
if ($errorMsg eq '') {
return '';
} else {
return $errorMsg;
}
}
sub oAuthConnect {
my ($this) = @_;
$this = RVL::RVGlobalSoft::singleton() if (!$this);
# Check Prem
RVL::RVGlobalSoft::Init::run();
# Restore Access key
my ($res) = $this->getAccesskey();
if ($res ne '') {
RVL::Session::set('message', $res);
RVL::Session::set('messageType', __CONSTANT__::RVL_MESSAGE_ERROR);
RVL::CpHandle::singleton()->redirectUrl();
}
my $authorizeIdFile = 'authorizeid.pub';
my $authorizekeyFile = 'authorizekey.pub';
my $isUser = RVL::CpHandle::singleton()->isUser();
if($isUser){
$authorizeIdFile = 'cp_authorizeid.pub';
$authorizekeyFile = 'cp_authorizekey.pub';
}
my ($accessKey, $authorizeId);
my ($homedir) = RVL::CpHandle::singleton()->getHomeDir();
# Check authorize id
if (-f $homedir . '/.rvglobalsoft/' . $authorizeIdFile) {
open(FD, '<', $homedir . '/.rvglobalsoft/' . $authorizeIdFile);
$authorizeId = join('', <FD>);
close(FD);
chomp ($authorizeId);
} else {
RVL::Session::set('message', 'Cannot found authorizeid.pub');
RVL::Session::set('messageType', __CONSTANT__::RVL_MESSAGE_ERROR);
RVL::CpHandle::singleton()->redirectUrl();
}
# Check authorize key
if (-f $homedir . '/.rvglobalsoft/' . $authorizekeyFile) {
open(FD, '<', $homedir . '/.rvglobalsoft/' . $authorizekeyFile);
$accessKey = join('', <FD>);
close(FD);
} else {
RVL::Session::set('message', 'Cannot found authorizekey.pub');
RVL::Session::set('messageType', __CONSTANT__::RVL_MESSAGE_ERROR);
RVL::CpHandle::singleton()->redirectUrl();
}
my ($oAuth) = RVL::oAuth::singleton({
'uid' => $authorizeId,
'url' => RVL::CpHandle::singleton()->getApiUrl(),
'publickey' => $accessKey,
});
if ($oAuth->isError()) {
my $error = $oAuth->getError();
RVL::Session::set('message', $error);
RVL::Session::set('messageType', __CONSTANT__::RVL_MESSAGE_ERROR);
RVL::CpHandle::singleton()->redirectUrl();
}
my ($token) = $oAuth->getToken();
if (!$token || $token eq '') {
RVL::Session::set('message', 'Cannot get token');
RVL::Session::set('messageType', __CONSTANT__::RVL_MESSAGE_ERROR);
RVL::CpHandle::singleton()->redirectUrl();
} else {
return $oAuth;
}
}
sub getRVGUserId {
my ($this) = shift;
my ($authorizeId, $username, $hostname) = @_;
my ($text);
my ($homedir) = RVL::CpHandle::singleton()->getHomeDir();
my $authorizeIdFile = 'authorizeid.pub';
my $authorizekeyFile = 'authorizekey.pub';
my $isUser = RVL::CpHandle::singleton()->isUser();
if($isUser)
{
$authorizeIdFile = 'cp_authorizeid.pub';
$authorizekeyFile = 'cp_authorizekey.pub';
}
my $authorizeIdFilePath = $homedir . '/.rvglobalsoft/' . $authorizeIdFile;
if($isUser){
if (-f $authorizeIdFilePath) {
$text = getUserID($authorizeIdFilePath);
}else{
$text = setUserID($authorizeIdFilePath,"$authorizeId$username$hostname");
}
}else{
$text = getUserID($authorizeIdFilePath);
}
return $text;
}
sub getUserID {
my $authorIdFile = $_[0];
my $authorizeId = '';
open(FD, '<',$authorIdFile);
$authorizeId = join('',<FD>);
chomp $authorizeId;
close(FD);
return $authorizeId;
}
sub setUserID {
my $authorIdFile = $_[0];
my $authorizeId = $_[1];
$authorizeId = 'cp_' . md5_hex($authorizeId);
open(FD, '>',$authorIdFile);
print FD $authorizeId;
close(FD);
return $authorizeId;
}
}
package RVL::RVGlobalSoft::Init;
use strict;
use warnings;
use Class::Std::Utils;
{
sub new {
my ($class, $opt) = @_;
my (%hash);
$hash{ENV} = \%ENV;
my ($this) = bless( \%hash, $class);
return $this;
}
sub run {
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
my ($req) = RVL::Request::singleton();
my ($managerName) = $req->getManagerName();
if (lc($managerName) ne 'notprivilege' && lc($managerName) ne 'notprivilegemgr') {
my ($oInit) = new RVL::RVGlobalSoft::Init();
return $oInit->checkOwnerPerm();
}
return 1;
}
sub checkOwnerPerm {
# Check Root Config RV Global Store API Key
my ($this) = shift;
RVL::logMessage('Check Root Config RV Global Store API Key', __CONSTANT__::RVL_LOG_DEBUG);
if ( !-f RVL::CpHandle::singleton()->getRootDir() . '/.rvglobalsoft/accesskey.pem'
|| !-f RVL::CpHandle::singleton()->getRootDir() . '/.rvglobalsoft/authorizeid.pub' ) {
$this->onError('Sorry, ROOT NOT configuration RVGlobalSoft API Key.' . "\n" . RVL::CpHandle::singleton()->getRootDir() . '/.rvglobalsoft/accesskey.pem'
. "\n" . RVL::CpHandle::singleton()->getRootDir() . '/.rvglobalsoft/authorizeid.pub');
return 0;
}
if (RVL::CpHandle::singleton()->getOwner() ne 'root') {
# Check Reseller Perm
RVL::logMessage('Check Reseller Perm', __CONSTANT__::RVL_LOG_DEBUG);
if ( !-f RVL::CpHandle::singleton()->getRootDir() . '/.rvglobalsoft/.allowreseller') {
$this->onError('Sorry, ROOT NOT allow reseller usage RVGlobalSoft.');
return 0;
}
}
# Check Owner Config RV Global Store API Key
RVL::logMessage('Check Owner Config RV Global Store API Key', __CONSTANT__::RVL_LOG_DEBUG);
if ( !-f RVL::CpHandle::singleton()->getOwnerDir() . '/.rvglobalsoft/authorizeid.pub'
|| !-f RVL::CpHandle::singleton()->getOwnerDir() . '/.rvglobalsoft/accesskey.pem' ) {
$this->onError('Sorry, Your owner NOT configuration RVGlobalSoft API Key.',);
return 0;
}
}
sub onError {
my ($this, $msg) = @_;
RVL::logMessage($msg, __CONSTANT__::RVL_LOG_DEBUG);
RVL::Session::set('message', $msg);
RVL::Session::set('messageType', __CONSTANT__::RVL_MESSAGE_ERROR);
RVL::CpHandle::singleton()->redirectUrl();
}
}
1;
__END__;
Copyright 2K16 - 2K18 Indonesian Hacker Rulez