/
10_named_slave_provisioning.pl
210 lines (174 loc) · 6.73 KB
/
10_named_slave_provisioning.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# i-MSCP Listener::Named::Slave::Provisioning 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
#
## Provides slave DNS server(s) provisioning service.
## This listener file requires i-MSCP 1.2.12 or newer.
## Slave provisioning service will be available at:
## - http://<panel.domain.tld>:8080/provisioning/slave_provisioning.php
## - https://<panel.domain.tld>:4443/provisioning/slave_provisioning.php (if you use ssl)
#
package Listener::Named::Slave::Provisioning;
use strict;
use warnings;
use iMSCP::Debug;
use iMSCP::Dir;
use iMSCP::EventManager;
use iMSCP::File;
use iMSCP::TemplateParser;
#
## HTTP (Basic) authentication parameters
## Those parameters are used to restrict access to the provisioning script which
## is available through HTTP(s)
#
# Authentication username
# Leave empty to disable authentication
my $authUsername = '';
# Authentication password
# Either an encrypted or plain password
# If an encrypted password, don't forget to set the $isAuthPasswordEncrypted parameter value to 1
my $authPassword = '';
# Tells whether or not the provided authentication password is encrypted
my $isAuthPasswordEncrypted = 0;
# Protected area identifier
my $realm = 'i-MSCP provisioning service for slave DNS servers';
#
## Other parameters
#
# Nginx configuration directory
my $nginxConfDir = '/etc/nginx';
#
## Please, don't edit anything below this line
#
# Create the .htpasswd file to restrict access to the provisioning script
sub createHtpasswdFile
{
if($authUsername =~ /:/) {
error("htpasswd: username contains illegal character ':'");
return 1;
}
require iMSCP::Crypt;
my $file = iMSCP::File->new( filename => "$main::imscpConfig{'GUI_PUBLIC_DIR'}/provisioning/.htpasswd" );
my $rs = $file->set(
"$authUsername:" . ($isAuthPasswordEncrypted ? $authPassword : iMSCP::Crypt::htpasswd($authPassword))
);
$rs ||= $file->save();
$rs ||= $file->owner(
"$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
"$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}"
);
$rs ||= $file->mode(0640);
}
#
## Event listeners
#
my $eventManager = iMSCP::EventManager->getInstance();
# Listener that is responsible to add authentication configuration
$eventManager->register('afterFrontEndBuildConfFile', sub {
my ($tplContent, $tplName) = @_;
return 0 unless $tplName eq '00_master.conf' || $tplName eq '00_master_ssl.conf';
my $locationSnippet = <<EOF;
location /provisioning {
root /var/www/imscp/gui/public;
location ~ \\.php\$ {
include imscp_fastcgi.conf;
satisfy any;
deny all;
auth_basic "$realm";
auth_basic_user_file $main::imscpConfig{'GUI_PUBLIC_DIR'}/provisioning/.htpasswd;
}
}
EOF
$$tplContent = replaceBloc(
"# SECTION custom BEGIN.\n",
"# SECTION custom END.\n",
" # SECTION custom BEGIN.\n" .
getBloc(
"# SECTION custom BEGIN.\n",
"# SECTION custom END.\n",
$$tplContent
) .
"$locationSnippet\n" .
" # SECTION custom END.\n",
$$tplContent
);
0;
}) if $authUsername;
# Listener that is responsible to create provisioning script
$eventManager->register('afterFrontEndInstall', sub {
my $fileContent = <<'EOF';
<?php
require '../../library/imscp-lib.php';
$config = iMSCP_Registry::get('config');
$filter = iMSCP_Registry::get('bufferFilter');
$filter->compressionInformation = false;
$masterDnsServerIp = $config['BASE_SERVER_PUBLIC_IP'];
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 { $masterDnsServerIp; };\n";
echo "\tallow-notify { $masterDnsServerIp; };\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 [$masterDnsServerIp]\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 { $masterDnsServerIp; };\n";
echo "\tallow-notify { $masterDnsServerIp; };\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 ALIASES LISTED ON $config->SERVER_HOSTNAME [$masterDnsServerIp\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 { $masterDnsServerIp; };\n";
echo "\tallow-notify { $masterDnsServerIp; };\n";
echo "};\n";
}
echo "// END ALIASES LIST\n";
}
EOF
my $rs = iMSCP::Dir->new( dirname => "$main::imscpConfig{'GUI_PUBLIC_DIR'}/provisioning" )->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
});
$rs ||= createHtpasswdFile() if $authUsername;
return $rs if $rs;
my $file = iMSCP::File->new( filename => "$main::imscpConfig{'GUI_PUBLIC_DIR'}/provisioning/slave_provisioning.php" );
$rs = $file->set($fileContent);
$rs ||= $file->save();
$rs ||= $file->owner(
"$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}",
"$main::imscpConfig{'SYSTEM_USER_PREFIX'}$main::imscpConfig{'SYSTEM_USER_MIN_UID'}"
);
$rs ||= $file->mode(0640);
});
1;
__END__