Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # i-MSCP Listener::Named::Zonetransfer listener file
- # Copyright (C) 2015 UncleJ, Arthur Mayer <mayer.arthur@gmail.com>
- # Copyright (C) 2016 Laurent Declercq <l.declercq@nuxwin.com>
- #
- # This library is free software; you can redistribute it and/or
- # modify it under the terms of the GNU Lesser General Public
- # License as published by the Free Software Foundation; either
- # version 2.1 of the License, or (at your option) any later version.
- #
- # This library is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- # Lesser General Public License for more details.
- #
- # You should have received a copy of the GNU Lesser General Public
- # License along with this library; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- #
- ## i-MSCP listener file that provides zone output for DNS zone transfer
- #
- package Listener::Named::Zonetransfer;
- use lib '/var/www/imscp/engine/PerlLib';
- use iMSCP::Bootstrapper;
- use iMSCP::Config;
- use iMSCP::Debug;
- use iMSCP::Dir;
- use iMSCP::EventManager;
- use iMSCP::Execute;
- use iMSCP::File;
- use File::Basename;
- #
- ## HTTP (Basic) authentication parameters
- ## Those parameters are used to protect access to the transfer script which is
- ## available through HTTP
- #
- # Authentication username
- # Leave empty to disable authentication
- my $authUsername = '';
- # Authentication password
- # Either an encrypted or plain password
- my $authPassword = '';
- # Tells wheter or not the provided authentication password is encrypted or not
- my $isAuthPasswordEncrypted = 0;
- # Protected area identifier
- my $realm = 'Secondary DNS service';
- #
- ### Subroutines
- #
- sub createHtpasswdFile
- {
- my $htpasswdFilePath = "$main::imscpConfig{'GUI_PUBLIC_DIR'}/domain/.htpasswd";
- my @cmd = (
- 'htpasswd',
- -f $htpasswdFilePath ? '-c' : '',
- '-b',
- $isAuthPasswordEncrypted ? '' : '-p',
- escapeShell($htpasswdFilePath),
- escapeShell($authUsername),
- escapeShell($authPassword)
- );
- my $rs = execute("@cmd", \my $stdout, \my $stderr);
- error($stderr) if $rs && $stderr;
- return $rs if $rs;
- my $htpasswdFile = iMSCP::File->new( filename => $htpasswdFilePath );
- my $rs = $htpasswdFile->mode(0640);
- $rs ||= $htpasswdFile->owner(
- "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
- "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}"
- );
- }
- sub createHtaccessFile
- {
- my $htaccessFile = iMSCP::File->new( filename => "$main::imscpConfig{'GUI_PUBLIC_DIR'}/domain/.htaccess" );
- my $htaccessFileContent = "<Files zone_transfer.php>\n";
- $htaccessFileContent .= "\tAuthType Basic\n";
- $htaccessFileContent .= ($realm) ? "\tAuthName \"$realm\"\n" : "\tAuthName \"zone_transfer.php\"\n";
- $htaccessFileContent .= "\tAuthUserFile $htpasswdFilePath\n";
- $htaccessFileContent .= "\tRequire user $authUsername\n";
- $htaccessFileContent .= "\tSatisfy all\n</Files>\n";
- my $rs = $htaccessFile->save();
- $rs ||= $htaccessFile->mode(0640);
- $rs ||= $htaccessFile->owner(
- "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
- "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}"
- );
- }
- sub writeTransferScript
- {
- my $fileContent = <<'EOF';
- <?php
- require '../../library/imscp-lib.php';
- $config = iMSCP_Registry::get('config');
- $filter = iMSCP_Registry::get('bufferFilter');
- $filter->compressionInformation = false;
- echo "// CONFIGURATION FOR MAIN DOMAIN\n";
- echo "zone \"$config->BASE_SERVER_VHOST\" {\n";
- echo "\ttype slave;\n";
- echo "\tfile \"/var/cache/bind/$config->BASE_SERVER_VHOST.db\";\n";
- echo "\tmasters { $config->BASE_SERVER_PUBLIC_IP; };\n";
- echo "\tallow-notify { $config->BASE_SERVER_PUBLIC_IP; };\n";
- echo "};\n";
- echo "// END CONFIGURATION FOR MAIN DOMAIN\n\n";
- $stmt = exec_query('SELECT domain_id, domain_name FROM domain');
- $rowCount = $stmt->rowCount();
- if ($rowCount > 0) {
- echo "// $rowCount HOSTED DOMAINS LISTED ON $config->SERVER_HOSTNAME [$config->BASE_SERVER_PUBLIC_IP]\n";
- while ($row = $stmt->fetchRow(PDO::FETCH_ASSOC)) {
- echo "zone \"{$row['domain_name']}\" {\n";
- echo "\ttype slave;\n";
- echo "\tfile \"/var/cache/bind/{$row['domain_name']}.db\";\n";
- echo "\tmasters { $config->BASE_SERVER_PUBLIC_IP; };\n";
- echo "\tallow-notify { $config->BASE_SERVER_PUBLIC_IP; };\n";
- echo "};\n";
- }
- echo "// END DOMAINS LIST\n\n";
- }
- $stmt = exec_query('SELECT alias_id, alias_name FROM domain_aliasses');
- $rowCount = $stmt->rowCount();
- if ($rowCount > 0) {
- echo "// $rowCount HOSTED ALIASSES LISTED ON $config->SERVER_HOSTNAME [$config->BASE_SERVER_PUBLIC_IP]\n";
- while ($row = $stmt->fetchRow(PDO::FETCH_ASSOC)) {
- echo "zone \"{$row['alias_name']}\" {\n";
- echo "\ttype slave;\n";
- echo "\tfile \"/var/cache/bind/{$row['alias_name']}.db\";\n";
- echo "\tmasters { $config->BASE_SERVER_PUBLIC_IP; };\n";
- echo "\tallow-notify { $config->BASE_SERVER_PUBLIC_IP; };\n";
- echo "};\n";
- }
- echo "// END ALIASSES LIST\n";
- }
- EOF
- my $transferScriptFile = iMSCP::File->new(
- filename => "$main::imscpConfig{'GUI_PUBLIC_DIR'}/domain/zone_transfer.php"
- );
- my $rs = $transferScriptFile->set($fileContent);
- $rs ||= $transferScriptFile->save();
- $rs ||= $transferScriptFile->mode(0640);
- $rs ||= $transferScriptFile->owner(
- "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
- "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}"
- );
- }
- iMSCP::Bootstrapper->getInstance()->boot({ nolock => 'yes', norequirements => 'yes', config_readonly => 'yes' });
- iMSCP::EventManager->getInstance()->register('afterInstall', sub {
- my $rs = iMSCP::Dir->new( dirname => $main::imscpConfig{'GUI_PUBLIC_DIR'}/domain )->make({
- user => "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
- group => "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
- mode => 0550
- });
- if($authUsername) {
- my $rs = createHtpasswdFile();
- $rs ||= createHtaccessFile()
- }
- $rs ||= writeTransferScript();
- });
- iMSCP::EventManager->getInstance()->trigger('afterInstall');
- 1;
- __END__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement