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 7.58 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. ## Slave provisioning service will be available at:
  22. ##   - http://<panel.domain.tld>:8080/domain/slave_provisioning.php
  23. ##   - https://<panel.domain.tld>:4443/domain/slave_provisioning.php (if you use ssl)
  24. #
  25.  
  26. package Listener::Named::SlaveProvisioning;
  27.  
  28. use lib '/var/www/imscp/engine/PerlLib';
  29. use iMSCP::Bootstrapper;
  30. use iMSCP::Config;
  31. use iMSCP::Debug;
  32. use iMSCP::Dir;
  33. use iMSCP::EventManager;
  34. use iMSCP::Execute;
  35. use iMSCP::File;
  36. use File::Basename;
  37.  
  38. #
  39. ## HTTP (Basic) authentication parameters
  40. ## Those parameters are used to protect access to the provisioning script which is
  41. ## available through HTTP
  42. #
  43.  
  44. # Authentication username
  45. # Leave empty to disable authentication
  46. my $authUsername = '';
  47.  
  48. # Authentication password
  49. # Either an encrypted or plain password
  50. my $authPassword = '';
  51.  
  52. # Tells whether or not the provided authentication password is encrypted or not
  53. my $isAuthPasswordEncrypted = 0;
  54.  
  55. # Protected area identifier
  56. my $realm = 'Provisioning service for slave DNS servers';
  57.  
  58. # Nginx configuration directory
  59. my $nginxConfDir = '/etc/nginx';
  60.  
  61. #
  62. ## Subroutines
  63. #
  64.  
  65. sub createHtpasswdFile
  66. {
  67.     my $htpasswdFilePath = "$main::imscpConfig{'GUI_PUBLIC_DIR'}/domain/.htpasswd";
  68.     my @cmd = (
  69.         'htpasswd',
  70.         -f $htpasswdFilePath ? '-c' : '',
  71.         '-b',
  72.         $isAuthPasswordEncrypted ? '' : '-p',
  73.         escapeShell($htpasswdFilePath),
  74.         escapeShell($authUsername),
  75.         escapeShell($authPassword)
  76.     );
  77.     my $rs = execute("@cmd", \my $stdout, \my $stderr);
  78.     error($stderr) if $rs && $stderr;
  79.     return $rs if $rs;
  80.  
  81.     my $htpasswdFile = iMSCP::File->new( filename => $htpasswdFilePath );
  82.     my $rs = $htpasswdFile->mode(0640);
  83.     $rs ||= $htpasswdFile->owner(
  84.         "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
  85.         "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}"
  86.     );
  87. }
  88.  
  89. #
  90. ## Event listeners
  91. #
  92.  
  93. my $eventManager = iMSCP::EventManager->getInstance();
  94.  
  95. # Listener that is responsible to create provisioning script
  96. $eventManager->register('afterInstall',, sub writeProvisioningScript {
  97.     my $fileContent = <<'EOF';
  98. <?php
  99.  
  100. require '../../library/imscp-lib.php';
  101.  
  102. $config = iMSCP_Registry::get('config');
  103. $filter = iMSCP_Registry::get('bufferFilter');
  104. $filter->compressionInformation = false;
  105.  
  106. echo "// CONFIGURATION FOR MAIN DOMAIN\n";
  107. echo "zone \"$config->BASE_SERVER_VHOST\" {\n";
  108. echo "\ttype slave;\n";
  109. echo "\tfile \"/var/cache/bind/$config->BASE_SERVER_VHOST.db\";\n";
  110. echo "\tmasters { $config->BASE_SERVER_PUBLIC_IP; };\n";
  111. echo "\tallow-notify { $config->BASE_SERVER_PUBLIC_IP; };\n";
  112. echo "};\n";
  113. echo "// END CONFIGURATION FOR MAIN DOMAIN\n\n";
  114.  
  115. $stmt = exec_query('SELECT domain_id, domain_name FROM domain');
  116. $rowCount = $stmt->rowCount();
  117.  
  118. if ($rowCount > 0) {
  119.     echo "// $rowCount HOSTED DOMAINS LISTED ON $config->SERVER_HOSTNAME [$config->BASE_SERVER_PUBLIC_IP]\n";
  120.  
  121.     while ($row = $stmt->fetchRow(PDO::FETCH_ASSOC)) {
  122.         echo "zone \"{$row['domain_name']}\" {\n";
  123.         echo "\ttype slave;\n";
  124.         echo "\tfile \"/var/cache/bind/{$row['domain_name']}.db\";\n";
  125.         echo "\tmasters { $config->BASE_SERVER_PUBLIC_IP; };\n";
  126.         echo "\tallow-notify { $config->BASE_SERVER_PUBLIC_IP; };\n";
  127.         echo "};\n";
  128.     }
  129.  
  130.     echo "// END DOMAINS LIST\n\n";
  131. }
  132.  
  133. $stmt = exec_query('SELECT alias_id, alias_name FROM domain_aliasses');
  134. $rowCount = $stmt->rowCount();
  135.  
  136. if ($rowCount > 0) {
  137.     echo "// $rowCount HOSTED ALIASSES LISTED ON $config->SERVER_HOSTNAME [$config->BASE_SERVER_PUBLIC_IP]\n";
  138.  
  139.     while ($row = $stmt->fetchRow(PDO::FETCH_ASSOC)) {
  140.         echo "zone \"{$row['alias_name']}\" {\n";
  141.         echo "\ttype slave;\n";
  142.         echo "\tfile \"/var/cache/bind/{$row['alias_name']}.db\";\n";
  143.         echo "\tmasters { $config->BASE_SERVER_PUBLIC_IP; };\n";
  144.         echo "\tallow-notify { $config->BASE_SERVER_PUBLIC_IP; };\n";
  145.         echo "};\n";
  146.     }
  147.  
  148.     echo "// END ALIASSES LIST\n";
  149. }
  150. EOF
  151.  
  152.     my $provisioningScriptFile = iMSCP::File->new(
  153.         filename => "$main::imscpConfig{'GUI_PUBLIC_DIR'}/domain/slave_provisioning.php"
  154.     );
  155.     my $rs = $provisioningScriptFile->set($fileContent);
  156.     $rs ||= $provisioningScriptFile->save();
  157.     $rs ||= $provisioningScriptFile->mode(0640);
  158.     $rs ||= $provisioningScriptFile->owner(
  159.         "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
  160.         "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}"
  161.     );
  162. });
  163.  
  164. # Listener that is responsible to add authentication configuration
  165. $eventManager->register('onAfterFrontEndBuildConf', sub onAfterFrontEndBuildConf) {
  166.     my ($tplContent, $tplName) = @_;
  167.  
  168.     unless(-d "$main::imscpConfig{'GUI_PUBLIC_DIR'}/domain") {
  169.         my $rs = iMSCP::Dir->new( dirname => "$main::imscpConfig{'GUI_PUBLIC_DIR'}/domain" )->make({
  170.             user => "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
  171.             group => "$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
  172.             mode => 0550
  173.         });
  174.  
  175.         $rs = createHtpasswdFile();
  176.         return $rs if $rs;
  177.     }
  178.  
  179.     if($tplName eq '00_master.conf' || $tplName eq '00_master_ssl.conf') {
  180.         (my $vhostName = $tplName) =~ s/(.*)\.conf$/;
  181.         my $includeFileName = $vhostName . "_protected_location.conf"
  182.         $$tplContent = replaceBloc(
  183.             "# SECTION custom BEGIN.\n",
  184.             "# SECTION custom END.\n",
  185.             "    # SECTION custom BEGIN.\n" .
  186.             getBloc(
  187.                 "# SECTION custom BEGIN.\n",
  188.                 "# SECTION custom END.\n",
  189.                 $$tplContent
  190.             ) .
  191.             "    include $includeFileName;\n" .
  192.             "    # SECTION custom END.\n",
  193.             $$tplContent
  194.         );
  195.  
  196.         my $fileContent;
  197.         $$fileContent = <<EOF;
  198. location /domain {
  199.     root /var/www/imscp/gui/public;
  200.  
  201.     location ~ \.php\$ {
  202.         include imscp_fastcgi.conf;
  203.         satisfy all;
  204.         allow  all;
  205.         auth_basic $realm;
  206.         auth_basic_user_file $main::imscpConfig{'GUI_PUBLIC_DIR'}/domain/.htpasswd;
  207.     }
  208. }
  209. EOF
  210.  
  211.         my $conffile = iMSCP::File->new( filename => "$nginxConfDir/$includeFileName" );
  212.         $rs = $conffile->set($$fileContent);
  213.         $rs ||= $conffile->save();
  214.         $rs ||= $conffile->mode(0644);
  215.         $rs ||= $conffile->owner($main::imscpConfig{'ROOT_USER'}, $main::imscpConfig{'ROOT_GROUP'});
  216.         return $rs if $rs;
  217.     }
  218. }) if $authUsername;
  219.  
  220. 1;
  221. __END__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement