Advertisement
Guest User

Untitled

a guest
Jan 16th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.59 KB | None | 0 0
  1. # i-MSCP Listener::Named::Zonetransfer listener file
  2. # Copyright (C) 2015 UncleJ, Arthur Mayer <mayer.arthur@gmail.com>
  3. # Copyright (C) 2016 Laurent Declercq <l.declercq@nuxwin.com>
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2.1 of the License, or (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public
  16. # License along with this library; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
  18.  
  19. #
  20. ## i-MSCP listener file that provides zone output for DNS zone transfer
  21. #
  22.  
  23. package Listener::Named::Zonetransfer;
  24.  
  25. use lib '/var/www/imscp/engine/PerlLib';
  26. use iMSCP::Bootstrapper;
  27. use iMSCP::Config;
  28. use iMSCP::Debug;
  29. use iMSCP::Dir;
  30. use iMSCP::EventManager;
  31. use iMSCP::Execute;
  32. use iMSCP::File;
  33. use File::Basename;
  34.  
  35. #
  36. ## HTTP (Basic) authentication parameters
  37. ## Those parameters are used to protect access to the transfer script which is
  38. ## available through HTTP
  39. #
  40.  
  41. # Authentication username
  42. # Leave empty to disable authentication
  43. my $authUsername = '';
  44.  
  45. # Authentication password
  46. # Either an encrypted or plain password
  47. my $authPassword = '';
  48.  
  49. # Tells wheter or not the provided authentication password is encrypted or not
  50. my $isAuthPasswordEncrypted = 0;
  51.  
  52. # Protected area identifier
  53. my $realm = 'Secondary DNS service';
  54.  
  55. #
  56. ### Subroutines
  57. #
  58.  
  59. sub createHtpasswdFile
  60. {
  61.     my $htpasswdFilePath = "$main::imscpConfig{'GUI_PUBLIC_DIR'}/domain/.htpasswd";
  62.     my @cmd = (
  63.         'htpasswd',
  64.         -f $htpasswdFilePath ? '-c' : '',
  65.         '-b',
  66.         $isAuthPasswordEncrypted ? '' : '-p',
  67.         escapeShell($htpasswdFilePath),
  68.         escapeShell($authUsername),
  69.         escapeShell($authPassword)
  70.     );
  71.     my $rs = execute("@cmd", \my $stdout, \my $stderr);
  72.     error($stderr) if $rs && $stderr;
  73.     return $rs if $rs;
  74.  
  75.     my $htpasswdFile = iMSCP::File->new( filename => $htpasswdFilePath );
  76.     my $rs = $htpasswdFile->mode(0640);
  77.     $rs ||= $htpasswdFile->owner(
  78.         "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
  79.         "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}"
  80.     );
  81. }
  82.  
  83. sub createHtaccessFile
  84. {
  85.     my $htaccessFile = iMSCP::File->new( filename => "$main::imscpConfig{'GUI_PUBLIC_DIR'}/domain/.htaccess" );
  86.     my $htaccessFileContent = "<Files zone_transfer.php>\n";
  87.     $htaccessFileContent .= "\tAuthType Basic\n";
  88.     $htaccessFileContent .= ($realm) ? "\tAuthName \"$realm\"\n" : "\tAuthName \"zone_transfer.php\"\n";
  89.     $htaccessFileContent .= "\tAuthUserFile $htpasswdFilePath\n";
  90.     $htaccessFileContent .= "\tRequire user $authUsername\n";
  91.     $htaccessFileContent .= "\tSatisfy all\n</Files>\n";
  92.  
  93.     my $rs = $htaccessFile->save();
  94.     $rs ||= $htaccessFile->mode(0640);
  95.     $rs ||= $htaccessFile->owner(
  96.         "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
  97.         "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}"
  98.     );
  99. }
  100.  
  101. sub writeTransferScript
  102. {
  103.     my $fileContent = <<'EOF';
  104. <?php
  105.  
  106. require '../../library/imscp-lib.php';
  107.  
  108. $config = iMSCP_Registry::get('config');
  109.  
  110. $filter = iMSCP_Registry::get('bufferFilter');
  111. $filter->compressionInformation = false;
  112.  
  113. echo "// CONFIGURATION FOR MAIN DOMAIN\n";
  114. echo "zone \"$config->BASE_SERVER_VHOST\" {\n";
  115. echo "\ttype slave;\n";
  116. echo "\tfile \"/var/cache/bind/$config->BASE_SERVER_VHOST.db\";\n";
  117. echo "\tmasters { $config->BASE_SERVER_PUBLIC_IP; };\n";
  118. echo "\tallow-notify { $config->BASE_SERVER_PUBLIC_IP; };\n";
  119. echo "};\n";
  120. echo "// END CONFIGURATION FOR MAIN DOMAIN\n\n";
  121.  
  122. $stmt = exec_query('SELECT domain_id, domain_name FROM domain');
  123. $rowCount = $stmt->rowCount();
  124.  
  125. if ($rowCount > 0) {
  126.     echo "// $rowCount HOSTED DOMAINS LISTED ON $config->SERVER_HOSTNAME [$config->BASE_SERVER_PUBLIC_IP]\n";
  127.  
  128.     while ($row = $stmt->fetchRow(PDO::FETCH_ASSOC)) {
  129.         echo "zone \"{$row['domain_name']}\" {\n";
  130.         echo "\ttype slave;\n";
  131.         echo "\tfile \"/var/cache/bind/{$row['domain_name']}.db\";\n";
  132.         echo "\tmasters { $config->BASE_SERVER_PUBLIC_IP; };\n";
  133.         echo "\tallow-notify { $config->BASE_SERVER_PUBLIC_IP; };\n";
  134.         echo "};\n";
  135.     }
  136.  
  137.     echo "// END DOMAINS LIST\n\n";
  138. }
  139.  
  140. $stmt = exec_query('SELECT alias_id, alias_name FROM domain_aliasses');
  141. $rowCount = $stmt->rowCount();
  142.  
  143. if ($rowCount > 0) {
  144.     echo "// $rowCount HOSTED ALIASSES LISTED ON $config->SERVER_HOSTNAME [$config->BASE_SERVER_PUBLIC_IP]\n";
  145.  
  146.     while ($row = $stmt->fetchRow(PDO::FETCH_ASSOC)) {
  147.         echo "zone \"{$row['alias_name']}\" {\n";
  148.         echo "\ttype slave;\n";
  149.         echo "\tfile \"/var/cache/bind/{$row['alias_name']}.db\";\n";
  150.         echo "\tmasters { $config->BASE_SERVER_PUBLIC_IP; };\n";
  151.         echo "\tallow-notify { $config->BASE_SERVER_PUBLIC_IP; };\n";
  152.         echo "};\n";
  153.     }
  154.  
  155.     echo "// END ALIASSES LIST\n";
  156. }
  157. EOF
  158.  
  159.     my $transferScriptFile = iMSCP::File->new(
  160.         filename => "$main::imscpConfig{'GUI_PUBLIC_DIR'}/domain/zone_transfer.php"
  161.     );
  162.     my $rs = $transferScriptFile->set($fileContent);
  163.     $rs ||= $transferScriptFile->save();
  164.     $rs ||= $transferScriptFile->mode(0640);
  165.     $rs ||= $transferScriptFile->owner(
  166.         "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
  167.         "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}"
  168.     );
  169. }
  170.  
  171. iMSCP::Bootstrapper->getInstance()->boot({ nolock => 'yes', norequirements => 'yes', config_readonly => 'yes' });
  172.  
  173. iMSCP::EventManager->getInstance()->register('afterInstall', sub {
  174.     my $rs = iMSCP::Dir->new( dirname => $main::imscpConfig{'GUI_PUBLIC_DIR'}/domain )->make({
  175.         user => "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
  176.         group => "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
  177.         mode => 0550
  178.     });
  179.  
  180.     if($authUsername) {
  181.         my $rs = createHtpasswdFile();
  182.         $rs ||= createHtaccessFile()
  183.     }
  184.  
  185.     $rs ||= writeTransferScript();
  186. });
  187.  
  188. iMSCP::EventManager->getInstance()->trigger('afterInstall');
  189.  
  190. 1;
  191. __END__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement