Advertisement
Guest User

Untitled

a guest
Jan 16th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.66 KB | None | 0 0
  1. # i-MSCP Listener::Named::SlaveProvisioning 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 output for slave DNS server provisioning
  21. #
  22.  
  23. package Listener::Named::SlaveProvisioning;
  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 provisioning 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 whether 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 slave_provisioning.php>\n";
  87.     $htaccessFileContent .= "\tAuthType Basic\n";
  88.     $htaccessFileContent .= ($realm) ? "\tAuthName \"$realm\"\n" : "\tAuthName \"slave_provisioning.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 writeProvisioningScript
  102. {
  103.     my $fileContent = <<'EOF';
  104. <?php
  105.  
  106. require '../../library/imscp-lib.php';
  107.  
  108. $config = iMSCP_Registry::get('config');
  109. $filter = iMSCP_Registry::get('bufferFilter');
  110. $filter->compressionInformation = false;
  111.  
  112. echo "// CONFIGURATION FOR MAIN DOMAIN\n";
  113. echo "zone \"$config->BASE_SERVER_VHOST\" {\n";
  114. echo "\ttype slave;\n";
  115. echo "\tfile \"/var/cache/bind/$config->BASE_SERVER_VHOST.db\";\n";
  116. echo "\tmasters { $config->BASE_SERVER_PUBLIC_IP; };\n";
  117. echo "\tallow-notify { $config->BASE_SERVER_PUBLIC_IP; };\n";
  118. echo "};\n";
  119. echo "// END CONFIGURATION FOR MAIN DOMAIN\n\n";
  120.  
  121. $stmt = exec_query('SELECT domain_id, domain_name FROM domain');
  122. $rowCount = $stmt->rowCount();
  123.  
  124. if ($rowCount > 0) {
  125.     echo "// $rowCount HOSTED DOMAINS LISTED ON $config->SERVER_HOSTNAME [$config->BASE_SERVER_PUBLIC_IP]\n";
  126.  
  127.     while ($row = $stmt->fetchRow(PDO::FETCH_ASSOC)) {
  128.         echo "zone \"{$row['domain_name']}\" {\n";
  129.         echo "\ttype slave;\n";
  130.         echo "\tfile \"/var/cache/bind/{$row['domain_name']}.db\";\n";
  131.         echo "\tmasters { $config->BASE_SERVER_PUBLIC_IP; };\n";
  132.         echo "\tallow-notify { $config->BASE_SERVER_PUBLIC_IP; };\n";
  133.         echo "};\n";
  134.     }
  135.  
  136.     echo "// END DOMAINS LIST\n\n";
  137. }
  138.  
  139. $stmt = exec_query('SELECT alias_id, alias_name FROM domain_aliasses');
  140. $rowCount = $stmt->rowCount();
  141.  
  142. if ($rowCount > 0) {
  143.     echo "// $rowCount HOSTED ALIASSES LISTED ON $config->SERVER_HOSTNAME [$config->BASE_SERVER_PUBLIC_IP]\n";
  144.  
  145.     while ($row = $stmt->fetchRow(PDO::FETCH_ASSOC)) {
  146.         echo "zone \"{$row['alias_name']}\" {\n";
  147.         echo "\ttype slave;\n";
  148.         echo "\tfile \"/var/cache/bind/{$row['alias_name']}.db\";\n";
  149.         echo "\tmasters { $config->BASE_SERVER_PUBLIC_IP; };\n";
  150.         echo "\tallow-notify { $config->BASE_SERVER_PUBLIC_IP; };\n";
  151.         echo "};\n";
  152.     }
  153.  
  154.     echo "// END ALIASSES LIST\n";
  155. }
  156. EOF
  157.  
  158.     my $provisioningScriptFile = iMSCP::File->new(
  159.         filename => "$main::imscpConfig{'GUI_PUBLIC_DIR'}/domain/slave_provisioning.php"
  160.     );
  161.     my $rs = $provisioningScriptFile->set($fileContent);
  162.     $rs ||= $provisioningScriptFile->save();
  163.     $rs ||= $provisioningScriptFile->mode(0640);
  164.     $rs ||= $provisioningScriptFile->owner(
  165.         "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
  166.         "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}"
  167.     );
  168. }
  169.  
  170. iMSCP::Bootstrapper->getInstance()->boot({ nolock => 'yes', norequirements => 'yes', config_readonly => 'yes' });
  171.  
  172. iMSCP::EventManager->getInstance()->register('afterInstall', sub {
  173.     my $rs = iMSCP::Dir->new( dirname => $main::imscpConfig{'GUI_PUBLIC_DIR'}/domain )->make({
  174.         user => "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
  175.         group => "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
  176.         mode => 0550
  177.     });
  178.  
  179.     if($authUsername) {
  180.         my $rs = createHtpasswdFile();
  181.         $rs ||= createHtaccessFile()
  182.     }
  183.  
  184.     $rs ||= writeProvisioningScript();
  185. });
  186.  
  187. iMSCP::EventManager->getInstance()->trigger('afterInstall');
  188.  
  189. 1;
  190. __END__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement