Skip to content

Commit

Permalink
Added DOMAIN_ID data (Alias, Domain, SubAlias and Subdomain modules)
Browse files Browse the repository at this point in the history
Several fixes
  • Loading branch information
nuxwin committed Jan 22, 2018
1 parent ec71bf0 commit b870f57
Show file tree
Hide file tree
Showing 36 changed files with 534 additions and 459 deletions.
9 changes: 6 additions & 3 deletions engine/PerlLib/iMSCP/Bootstrapper.pm
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ sub boot
}

unless ( $options->{'norequirements'} ) {
my $test = ( iMSCP::Getopt->context() eq 'installer' ) ? 'all' : 'user';
iMSCP::Requirements->new()->$test();
if ( iMSCP::Getopt->context() eq 'installer' ) {
iMSCP::Requirements->new()->all();
} else {
iMSCP::Requirements->new()->user();
}
}

$self->_genKeys() unless $options->{'nokeys'};
Expand Down Expand Up @@ -122,7 +125,7 @@ sub loadMainConfig
Lock a file
Param bool $nowait OPTIONAL Whether or not to wait for lock (Default: FALSE)
Param bool $nowait OPTIONAL Whether or not to wait for lock
Return int 1 if lock file has been acquired, 0 if lock file has not been acquired (nowait case), croak on failure
=cut
Expand Down
6 changes: 5 additions & 1 deletion engine/PerlLib/iMSCP/Composer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ sub new
"prefer-stable":true
}
EOT
$self->{'_php_cmd'} = [ '/usr/bin/php7.1', '-d', "date.timezone=$main::imscpConfig{'TIMEZONE'}", '-d', 'allow_url_fopen=1' ];

$self->{'_php_cmd'} = [
( iMSCP::ProgramFinder::find( 'php' ) or die( "Couldn't find system PHP (cli) binary" ) ),
'-d', "date.timezone=$main::imscpConfig{'TIMEZONE'}", '-d', 'allow_url_fopen=1'
];
# Set default STD routines
$self->setStdRoutines();
}
Expand Down
34 changes: 3 additions & 31 deletions engine/PerlLib/iMSCP/Modules/Alias.pm
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,6 @@ sub process
$rs;
}

=item disable( )
Disable domain alias
Return int 0 on success, other on failure
=cut

sub disable
{
my ($self) = @_;

eval {
local $self->{'_dbh'}->{'RaiseError'} = 1;

# Sets the status of any subdomain that belongs to this domain alias to 'todisable'.
$self->{'_dbh'}->do(
"UPDATE subdomain_alias SET subdomain_alias_status = 'todisable' WHERE alias_id = ? AND subdomain_alias_status <> 'todelete'",
undef, $self->{'alias_id'}
);
};
if ( $@ ) {
error( $@ );
return 1;
}

$self->SUPER::disable();
}

=back
=head1 PRIVATE METHODS
Expand Down Expand Up @@ -237,6 +208,7 @@ sub _getData
BASE_SERVER_IP => $main::imscpConfig{'BASE_SERVER_IP'},
BASE_SERVER_PUBLIC_IP => $main::imscpConfig{'BASE_SERVER_PUBLIC_IP'},
DOMAIN_ADMIN_ID => $self->{'domain_admin_id'},
DOMAIN_ID => $self->{'alias_id'},
DOMAIN_NAME => $self->{'alias_name'},
DOMAIN_IP => $main::imscpConfig{'BASE_SERVER_IP'} eq '0.0.0.0' ? '0.0.0.0' : $self->{'ip_number'},
DOMAIN_TYPE => 'als',
Expand All @@ -262,8 +234,8 @@ sub _getData
FORWARD => $self->{'url_forward'} || 'no',
FORWARD_TYPE => $self->{'type_forward'} || '',
FORWARD_PRESERVE_HOST => $self->{'host_forward'} || 'Off',
DISABLE_FUNCTIONS =>
$phpini->{'disable_functions'} || 'exec,passthru,phpinfo,popen,proc_open,show_source,shell,shell_exec,symlink,system',
DISABLE_FUNCTIONS => $phpini->{'disable_functions'}
|| 'exec,passthru,phpinfo,popen,proc_open,show_source,shell,shell_exec,symlink,system',
MAX_EXECUTION_TIME => $phpini->{'max_execution_time'} || 30,
MAX_INPUT_TIME => $phpini->{'max_input_time'} || 60,
MEMORY_LIMIT => $phpini->{'memory_limit'} || 128,
Expand Down
202 changes: 4 additions & 198 deletions engine/PerlLib/iMSCP/Modules/Domain.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,10 @@ package iMSCP::Modules::Domain;
use strict;
use warnings;
use Carp qw/ croak /;
use File::Basename;
use File::Spec;
use File::Temp;
use iMSCP::Crypt qw/ decryptRijndaelCBC randomStr /;
use iMSCP::Debug qw/ debug error getLastError warning /;
use iMSCP::Dir;
use iMSCP::Ext2Attributes qw/ clearImmutable /;
use iMSCP::Execute qw/ execute escapeShell /;
use iMSCP::Servers::Sqld;
use iMSCP::Debug qw/ error getLastError warning /;
use parent 'iMSCP::Modules::Abstract';

# See _restoreDatabase() below
my $DEFAULT_MYSQL_CONFFILE;

=head1 DESCRIPTION
Module for processing of SSL domain entities
Expand Down Expand Up @@ -113,138 +103,6 @@ sub process
$rs;
}

=item disable( )
Disable domain
Return int 0 on success, other on failure
=cut

sub disable
{
my ($self) = @_;

eval {
local $self->{'_dbh'}->{'RaiseError'} = 1;

# Sets the status of any subdomain that belongs to this domain to 'todisable'.
$self->{'_dbh'}->do(
"UPDATE subdomain SET subdomain_status = 'todisable' WHERE domain_id = ? AND subdomain_status <> 'todelete'", undef, $self->{'domain_id'}
);
};
if ( $@ ) {
error( $@ );
return 1;
}

$self->SUPER::disable();
}

=item restore( )
Restore backup
Return int 0 on success, other on failure
=cut

sub restore
{
my ($self) = @_;

my $homeDir = "$main::imscpConfig{'USER_WEB_DIR'}/$self->{'domain_name'}";
my $bkpDir = "$homeDir/backups";

eval {
# Restore know databases only
local $self->{'_dbh'}->{'RaiseError'} = 1;

my $rows = $self->{'_dbh'}->selectall_arrayref(
'SELECT sqld_name FROM sql_database WHERE domain_id = ?', { Slice => {} }, $self->{'domain_id'}
);

for my $row( @{$rows} ) {
# Encode slashes as SOLIDUS unicode character
# Encode dots as Full stop unicode character
( my $encodedDbName = $row->{'sqld_name'} ) =~ s%([./])%{ '/', '@002f', '.', '@002e' }->{$1}%ge;

for ( '.sql', '.sql.bz2', '.sql.gz', '.sql.lzma', '.sql.xz' ) {
my $dbDumpFilePath = File::Spec->catfile( $bkpDir, $encodedDbName . $_ );
debug( $dbDumpFilePath );
next unless -f $dbDumpFilePath;
$self->_restoreDatabase( $row->{'sqld_name'}, $dbDumpFilePath );
}
}

# Restore first Web backup found
for ( iMSCP::Dir->new( dirname => $bkpDir )->getFiles() ) {
next if -l "$bkpDir/$_"; # Don't follow symlinks (See #IP-990)
next unless /^web-backup-.+?\.tar(?:\.(bz2|gz|lzma|xz))?$/;

my $archFormat = $1 || '';

# Since we are now using immutable bit to protect some folders, we must in order do the following
# to restore a backup archive:
#
# - Un-protect user homedir (clear immutable flag recursively)
# - Restore web files
# - Update status of sub, als and alssub, entities linked to the parent domain to 'torestore'
# - Run the restore( ) parent method
#
# The third and last tasks allow the i-MSCP Httpd server implementations to set correct permissions and
# set immutable flag on folders if needed for each entity
#
# Note: This is a lot of works but this will be fixed when the backup feature will be rewritten

if ( $archFormat eq 'bz2' ) {
$archFormat = 'bzip2';
} elsif ( $archFormat eq 'gz' ) {
$archFormat = 'gzip';
}

clearImmutable( $homeDir, 1 ); # Un-protect homedir recursively

my $cmd;
if ( $archFormat ne '' ) {
$cmd = [ 'tar', '-x', '-p', "--$archFormat", '-C', $homeDir, '-f', "$bkpDir/$_" ];
} else {
$cmd = [ 'tar', '-x', '-p', '-C', $homeDir, '-f', "$bkpDir/$_" ];
}

my $rs = execute( $cmd, \ my $stdout, \ my $stderr );
debug( $stdout ) if $stdout;
$rs == 0 or croak( $stderr || 'Unknown error' );

eval {
$self->{'_dbh'}->begin_work();
$self->{'_dbh'}->do(
'UPDATE subdomain SET subdomain_status = ? WHERE domain_id = ?', undef, 'torestore', $self->{'domain_id'}
);
$self->{'_dbh'}->do( 'UPDATE domain_aliasses SET alias_status = ? WHERE domain_id = ?', undef, 'torestore', $self->{'domain_id'} );
$self->{'_dbh'}->do(
"
UPDATE subdomain_alias
SET subdomain_alias_status = 'torestore'
WHERE alias_id IN (SELECT alias_id FROM domain_aliasses WHERE domain_id = ?)
",
undef, $self->{'domain_id'}
);
$self->{'_dbh'}->commit();
};

$self->{'_dbh'}->rollback() if $@;
last;
}
};
if ( $@ ) {
error( $@ );
return 1;
}

$self->SUPER::restore();
}

=back
=head1 PRIVATE METHODS
Expand Down Expand Up @@ -344,6 +202,7 @@ sub _getData
BASE_SERVER_IP => $main::imscpConfig{'BASE_SERVER_IP'},
BASE_SERVER_PUBLIC_IP => $main::imscpConfig{'BASE_SERVER_PUBLIC_IP'},
DOMAIN_ADMIN_ID => $self->{'domain_admin_id'},
DOMAIN_ID => $self->{'domain_id'},
DOMAIN_NAME => $self->{'domain_name'},
DOMAIN_IP => $main::imscpConfig{'BASE_SERVER_IP'} eq '0.0.0.0' ? '0.0.0.0' : $self->{'ip_number'},
DOMAIN_TYPE => 'dmn',
Expand All @@ -369,8 +228,8 @@ sub _getData
FORWARD => $self->{'url_forward'} || 'no',
FORWARD_TYPE => $self->{'type_forward'} || '',
FORWARD_PRESERVE_HOST => $self->{'host_forward'} || 'Off',
DISABLE_FUNCTIONS =>
$phpini->{'disable_functions'} || 'exec,passthru,phpinfo,popen,proc_open,show_source,shell,shell_exec,symlink,system',
DISABLE_FUNCTIONS => $phpini->{'disable_functions'}
|| 'exec,passthru,phpinfo,popen,proc_open,show_source,shell,shell_exec,symlink,system',
MAX_EXECUTION_TIME => $phpini->{'max_execution_time'} || 30,
MAX_INPUT_TIME => $phpini->{'max_input_time'} || 60,
MEMORY_LIMIT => $phpini->{'memory_limit'} || 128,
Expand All @@ -385,59 +244,6 @@ sub _getData
};
}

=item _restoreDatabase( $dbName, $dbDumpFilePath )
Restore a database from the given database dump file
Param string $dbName Database name
Param string $dbDumpFilePath Path to database dump file
Return void, croak on failure
=cut

sub _restoreDatabase
{
my (undef, $dbName, $dbDumpFilePath) = @_;

my (undef, undef, $archFormat) = fileparse( $dbDumpFilePath, qr/\.(?:bz2|gz|lzma|xz)/ );

my $cmd;

if ( $archFormat eq '.bz2' ) {
$cmd = 'bzcat -d ';
} elsif ( $archFormat eq '.gz' ) {
$cmd = 'zcat -d ';
} elsif ( $archFormat eq '.lzma' ) {
$cmd = 'lzma -dc ';
} elsif ( $archFormat eq '.xz' ) {
$cmd = 'xz -dc ';
} else {
$cmd = 'cat ';
}

unless ( $DEFAULT_MYSQL_CONFFILE ) {
$DEFAULT_MYSQL_CONFFILE = File::Temp->new();
print $DEFAULT_MYSQL_CONFFILE <<"EOF";
[mysql]
host = $main::imscpConfig{'DATABASE_HOST'}
port = $main::imscpConfig{'DATABASE_PORT'}
user = "@{ [ $main::imscpConfig{'DATABASE_USER'} =~ s/"/\\"/gr ] }"
password = "@{ [ decryptRijndaelCBC($main::imscpKEY, $main::imscpIV, $main::imscpConfig{'DATABASE_PASSWORD'}) =~ s/"/\\"/gr ] }"
max_allowed_packet = 500M
EOF
$DEFAULT_MYSQL_CONFFILE->close();
}

my @cmd = (
$cmd, escapeShell( $dbDumpFilePath ), '|', "mysql --defaults-extra-file=$DEFAULT_MYSQL_CONFFILE",
escapeShell( $dbName )
);
my $rs = execute( "@cmd", \ my $stdout, \ my $stderr );
debug( $stdout ) if $stdout;
$rs == 0 or croak( error( sprintf( "Couldn't restore SQL database: %s", $stderr || 'Unknown error' )));
0;
}

=back
=head1 AUTHOR
Expand Down
8 changes: 4 additions & 4 deletions engine/PerlLib/iMSCP/Modules/SubAlias.pm
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ sub _getData
'SELECT * FROM php_ini WHERE domain_id = ? AND domain_type = ?',
undef,
( $self->{'php_config_level'} eq 'per_user'
? $self->{'domain_id'}
: ( $self->{'php_config_level'} eq 'per_domain' ? $self->{'subdomain_alias_id'} : $self->{'subdomain_id'} )
? $self->{'domain_id'} : ( $self->{'php_config_level'} eq 'per_domain' ? $self->{'alias_id'} : $self->{'subdomain_alias_id'} )
),
( $self->{'php_config_level'} eq 'per_user' ? 'dmn' : ( $self->{'php_config_level'} eq 'per_domain' ? 'als' : 'subals' ) )
) || {};
Expand All @@ -217,6 +216,7 @@ sub _getData
BASE_SERVER_IP => $main::imscpConfig{'BASE_SERVER_IP'},
BASE_SERVER_PUBLIC_IP => $main::imscpConfig{'BASE_SERVER_PUBLIC_IP'},
DOMAIN_ADMIN_ID => $self->{'domain_admin_id'},
DOMAIN_ID => $self->{'subdomain_alias_id'},
DOMAIN_NAME => $self->{'subdomain_alias_name'} . '.' . $self->{'alias_name'},
DOMAIN_IP => $main::imscpConfig{'BASE_SERVER_IP'} eq '0.0.0.0' ? '0.0.0.0' : $self->{'ip_number'},
DOMAIN_TYPE => 'alssub',
Expand Down Expand Up @@ -244,8 +244,8 @@ sub _getData
FORWARD => $self->{'subdomain_alias_url_forward'} || 'no',
FORWARD_TYPE => $self->{'subdomain_alias_type_forward'} || '',
FORWARD_PRESERVE_HOST => $self->{'subdomain_alias_host_forward'} || 'Off',
DISABLE_FUNCTIONS =>
$phpini->{'disable_functions'} || 'exec,passthru,phpinfo,popen,proc_open,show_source,shell,shell_exec,symlink,system',
DISABLE_FUNCTIONS => $phpini->{'disable_functions'}
|| 'exec,passthru,phpinfo,popen,proc_open,show_source,shell,shell_exec,symlink,system',
MAX_EXECUTION_TIME => $phpini->{'max_execution_time'} || 30,
MAX_INPUT_TIME => $phpini->{'max_input_time'} || 60,
MEMORY_LIMIT => $phpini->{'memory_limit'} || 128,
Expand Down
5 changes: 3 additions & 2 deletions engine/PerlLib/iMSCP/Modules/Subdomain.pm
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ sub _getData
BASE_SERVER_IP => $main::imscpConfig{'BASE_SERVER_IP'},
BASE_SERVER_PUBLIC_IP => $main::imscpConfig{'BASE_SERVER_PUBLIC_IP'},
DOMAIN_ADMIN_ID => $self->{'domain_admin_id'},
DOMAIN_ID => $self->{'subdomain_id'},
DOMAIN_NAME => $self->{'subdomain_name'} . '.' . $self->{'user_home'},
DOMAIN_IP => $main::imscpConfig{'BASE_SERVER_IP'} eq '0.0.0.0' ? '0.0.0.0' : $self->{'ip_number'},
DOMAIN_TYPE => 'sub',
Expand All @@ -232,8 +233,8 @@ sub _getData
FORWARD => $self->{'subdomain_url_forward'} || 'no',
FORWARD_TYPE => $self->{'subdomain_type_forward'} || '',
FORWARD_PRESERVE_HOST => $self->{'subdomain_host_forward'} || 'Off',
DISABLE_FUNCTIONS =>
$phpini->{'disable_functions'} || 'exec,passthru,phpinfo,popen,proc_open,show_source,shell,shell_exec,symlink,system',
DISABLE_FUNCTIONS => $phpini->{'disable_functions'}
|| 'exec,passthru,phpinfo,popen,proc_open,show_source,shell,shell_exec,symlink,system',
MAX_EXECUTION_TIME => $phpini->{'max_execution_time'} || 30,
MAX_INPUT_TIME => $phpini->{'max_input_time'} || 60,
MEMORY_LIMIT => $phpini->{'memory_limit'} || 128,
Expand Down
6 changes: 5 additions & 1 deletion engine/PerlLib/iMSCP/Modules/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ sub process
sub add
{
my ($self) = @_;


# TODO: To be moved in the iMSCP::Servers::Server implementation

return $self->SUPER::add() if $self->{'admin_status'} eq 'tochangepwd';

my $user = my $group = $main::imscpConfig{'SYSTEM_USER_PREFIX'} . ( $main::imscpConfig{'SYSTEM_USER_MIN_UID'}+$self->{'admin_id'} );
Expand Down Expand Up @@ -153,6 +155,8 @@ sub delete
{
my ($self) = @_;

# TODO: To be moved in the iMSCP::Servers::Server implementation

my $user = my $group = $main::imscpConfig{'SYSTEM_USER_PREFIX'} . ( $main::imscpConfig{'SYSTEM_USER_MIN_UID'}+$self->{'admin_id'} );
my $rs = $self->{'eventManager'}->trigger( 'onBeforeDeleteImscpUnixUser', $user );
$rs ||= $self->SUPER::delete();
Expand Down

0 comments on commit b870f57

Please sign in to comment.