Advertisement
Guest User

Untitled

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