/
Abstract.pm
1196 lines (876 loc) · 39 KB
/
Abstract.pm
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=head1 NAME
iMSCP::Servers::Named::Bind9::Abstract - i-MSCP Bind9 Server abstract implementation
=cut
# i-MSCP - internet Multi Server Control Panel
# Copyright (C) 2010-2018 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 St, Fifth Floor, Boston, MA 02110-1301 USA
package iMSCP::Servers::Named::Bind9::Abstract;
use strict;
use warnings;
use autouse 'iMSCP::Rights' => qw/ setRights /;
use autouse 'iMSCP::Dialog::InputValidation' => qw/ isOneOfStringsInList isStringInList /;
use Carp qw/ croak /;
use Class::Autouse qw/ :nostat iMSCP::Getopt /;
use File::Basename;
use iMSCP::Boolean;
use iMSCP::Debug qw/ debug /;
use iMSCP::Dir;
use iMSCP::Execute qw/ execute /;
use iMSCP::File;
use iMSCP::Getopt;
use iMSCP::Net;
use iMSCP::Template::Processor qw/ getBlocByRef processBlocByRef processVarsByRef processVars /;
use iMSCP::Umask;
use version;
use parent 'iMSCP::Servers::Named';
=head1 DESCRIPTION
i-MSCP Bind9 Server abstract implementation.
=head1 PUBLIC METHODS
=over 4
=item registerSetupListeners( )
See iMSCP::Servers::Abstract::RegisterSetupListeners()
=cut
sub registerSetupListeners
{
my ( $self ) = @_;
$self->{'eventManager'}->registerOne(
'beforeSetupDialog',
sub { push @{ $_[0] }, sub { $self->askDnsServerMode( @_ ) }, sub { $self->askIPv6Support( @_ ) }, sub { $self->askLocalDnsResolver( @_ ) }; },
$self->getServerPriority()
);
}
=item askDnsServerMode( \%dialog )
Ask user for DNS server type to configure
Param iMSCP::Dialog \%dialog
Return int 0 (NEXT), 30 (BACK) or 50 (ESC)
=cut
sub askDnsServerMode
{
my ( $self, $dialog ) = @_;
my $value = ::setupGetQuestion( 'NAMED_MODE', $self->{'config'}->{'NAMED_MODE'} || ( iMSCP::Getopt->preseed ? 'master' : '' ));
my %choices = ( 'master', 'Master DNS server', 'slave', 'Slave DNS server' );
if ( isOneOfStringsInList( iMSCP::Getopt->reconfigure, [ 'named', 'servers', 'all', 'forced' ] ) || !isStringInList( $value, keys %choices ) ) {
( my $rs, $value ) = $dialog->radiolist( <<"EOF", \%choices, ( grep ( $value eq $_, keys %choices ) )[0] || 'master' );
Please choose the type of DNS server to configure:
\\Z \\Zn
EOF
return $rs unless $rs < 30;
}
::setupSetQuestion( 'NAMED_MODE', $value );
$self->{'config'}->{'NAMED_MODE'} = $value;
$self->askDnsServerIps( $dialog );
}
=item askDnsServerIps( \%dialog )
Ask user for DNS server adresses IP
Param iMSCP::Dialog \%dialog
Return int 0 (NEXT), 30 (BACK) or 50 (ESC)
=cut
sub askDnsServerIps
{
my ( $self, $dialog ) = @_;
my $dnsServerMode = $self->{'config'}->{'NAMED_MODE'};
my @masterDnsIps = split /[; \t]+/, ::setupGetQuestion(
'NAMED_PRIMARY_DNS', $self->{'config'}->{'NAMED_PRIMARY_DNS'} || ( iMSCP::Getopt->preseed ? 'no' : '' )
);
my @slaveDnsIps = split /[; \t]+/, ::setupGetQuestion(
'NAMED_SECONDARY_DNS', $self->{'config'}->{'NAMED_SECONDARY_DNS'} || ( iMSCP::Getopt->preseed ? 'no' : '' )
);
my ( $rs, $answer, $msg ) = ( 0, '', '' );
if ( $dnsServerMode eq 'master' ) {
if ( isOneOfStringsInList( iMSCP::Getopt->reconfigure, [ 'named', 'servers', 'all', 'forced' ] ) || !@slaveDnsIps ||
( $slaveDnsIps[0] ne 'no' && !$self->_checkIps( @slaveDnsIps ) )
) {
my %choices = ( 'yes', 'Yes', 'no', 'No' );
( $rs, $answer ) = $dialog->radiolist( <<"EOF", \%choices, !@slaveDnsIps || $slaveDnsIps[0] eq 'no' ? 'no' : 'yes' );
Do you want to add slave DNS servers?
\\Z \\Zn
EOF
if ( $rs < 30 && $answer eq 'yes' ) {
@slaveDnsIps = () if @slaveDnsIps && $slaveDnsIps[0] eq 'no';
do {
( $rs, $answer ) = $dialog->inputbox( <<"EOF", join ' ', @slaveDnsIps );
$msg
Please enter the IP addresses for the slave DNS servers, each separated by a space or semicolon:
EOF
$msg = '';
if ( $rs < 30 ) {
@slaveDnsIps = split /[; ]+/, $answer;
if ( !@slaveDnsIps ) {
$msg = <<"EOF";
\\Z1You must enter at least one IP address.\\Zn
EOF
} elsif ( !$self->_checkIps( @slaveDnsIps ) ) {
$msg = <<"EOF"
\\Z1Wrong or disallowed IP address found.\\Zn
EOF
}
}
} while $rs < 30 && $msg;
} else {
@slaveDnsIps = ( 'no' );
}
}
} elsif ( isOneOfStringsInList( iMSCP::Getopt->reconfigure, [ 'named', 'servers', 'all', 'forced' ] ) || !@slaveDnsIps || $slaveDnsIps[0] eq 'no'
|| !$self->_checkIps( @masterDnsIps )
) {
@masterDnsIps = () if @masterDnsIps && $masterDnsIps[0] eq 'no';
do {
( $rs, $answer ) = $dialog->inputbox( <<"EOF", join ' ', @masterDnsIps );
$msg
Please enter the IP addresses for the master DNS server, each separated by space or semicolon:
EOF
$msg = '';
if ( $rs < 30 ) {
@masterDnsIps = split /[; ]+/, $answer;
if ( !@masterDnsIps ) {
$msg = <<"EOF";
\\Z1You must enter a least one IP address.\\Zn
EOF
} elsif ( !$self->_checkIps( @masterDnsIps ) ) {
$msg = <<"EOF";
\\Z1Wrong or disallowed IP address found.\\Zn
EOF
}
}
} while $rs < 30 && $msg;
}
return $rs unless $rs < 30;
if ( $dnsServerMode eq 'master' ) {
$self->{'config'}->{'NAMED_PRIMARY_DNS'} = 'no';
$self->{'config'}->{'NAMED_SECONDARY_DNS'} = join ';', @slaveDnsIps;
return $rs;
}
$self->{'config'}->{'NAMED_PRIMARY_DNS'} = join ';', @masterDnsIps;
::setupSetQuestion( 'NAMED_PRIMARY_DNS', $self->{'config'}->{'NAMED_PRIMARY_DNS'} );
::setupSetQuestion( 'NAMED_SECONDARY_DNS', 'no' );
$self->{'config'}->{'NAMED_SECONDARY_DNS'} = 'no';
$rs;
}
=item askIPv6Support( \%dialog )
Ask user for DNS server IPv6 support
Param iMSCP::Dialog \%dialog
Return int 0 (NEXT), 30 (BACK) or 50 (ESC)
=cut
sub askIPv6Support
{
my ( $self, $dialog ) = @_;
unless ( ::setupGetQuestion( 'IPV6_SUPPORT' ) eq 'yes' ) {
::setupSetQuestion( 'NAMED_IPV6_SUPPORT', 'no' );
$self->{'config'}->{'NAMED_IPV6_SUPPORT'} = 'no';
return 0;
}
my $value = ::setupGetQuestion( 'NAMED_IPV6_SUPPORT', $self->{'config'}->{'NAMED_IPV6_SUPPORT'} || ( iMSCP::Getopt->preseed ? 'no' : '' ));
my %choices = ( 'yes', 'Yes', 'no', 'No' );
if ( isOneOfStringsInList( iMSCP::Getopt->reconfigure, [ 'named', 'servers', 'all', 'forced' ] ) || !isStringInList( $value, keys %choices ) ) {
( my $rs, $value ) = $dialog->radiolist( <<"EOF", \%choices, ( grep ( $value eq $_, keys %choices ) )[0] || 'no' );
Do you want to enable IPv6 support for the DNS server?
\\Z \\Zn
EOF
return $rs unless $rs < 30;
}
::setupSetQuestion( 'NAMED_IPV6_SUPPORT', $value );
$self->{'config'}->{'NAMED_IPV6_SUPPORT'} = $value;
0;
}
=item askLocalDnsResolver( \%dialog )
Ask user for local DNS resolver
Param iMSCP::Dialog \%dialog
Return int 0 (NEXT), 30 (BACK) or 50 (ESC)
=cut
sub askLocalDnsResolver
{
my ( $self, $dialog ) = @_;
my $value = ::setupGetQuestion(
'NAMED_LOCAL_DNS_RESOLVER', $self->{'config'}->{'NAMED_LOCAL_DNS_RESOLVER'} || ( iMSCP::Getopt->preseed ? 'yes' : '' )
);
my %choices = ( 'yes', 'Yes', 'no', 'No' );
if ( isOneOfStringsInList( iMSCP::Getopt->reconfigure, [ 'resolver', 'named', 'servers', 'all', 'forced' ] )
|| !isStringInList( $value, keys %choices )
) {
( my $rs, $value ) = $dialog->radiolist( <<"EOF", \%choices, ( grep ( $value eq $_, keys %choices ) )[0] || 'yes' );
Do you want to use Bind9 as local DNS resolver?
\\Z \\Zn
EOF
return $rs unless $rs < 30;
}
$self->{'config'}->{'NAMED_LOCAL_DNS_RESOLVER'} = $value;
0;
}
=item install( )
See iMSCP::Servers::Abstract::install()
=cut
sub install
{
my ( $self ) = @_;
for my $conffile ( 'NAMED_CONF_FILE', 'NAMED_LOCAL_CONF_FILE', 'NAMED_OPTIONS_CONF_FILE' ) {
next unless length $self->{'config'}->{$conffile};
$self->_bkpConfFile( $self->{'config'}->{$conffile} );
}
$self->_setVersion();
$self->_makeDirs();
$self->_configure();
}
=item setBackendPermissions( )
See iMSCP::Servers::Abstract::setBackendPermissions()
=cut
sub setBackendPermissions
{
my ( $self ) = @_;
setRights( $self->{'config'}->{'NAMED_CONF_DIR'}, {
user => $::imscpConfig{'ROOT_USER'},
group => $self->{'config'}->{'NAMED_GROUP'},
dirmode => '2750',
filemode => '0640',
recursive => TRUE
} );
setRights( $self->{'config'}->{'NAMED_DB_ROOT_DIR'}, {
user => $self->{'config'}->{'NAMED_USER'},
group => $self->{'config'}->{'NAMED_GROUP'},
dirmode => '2750',
filemode => '0640',
recursive => TRUE
} );
}
=item getServerName( )
See iMSCP::Servers::Abstract::getServerName()
=cut
sub getServerName
{
my ( $self ) = @_;
'Bind';
}
=item getServerHumanName( )
See iMSCP::Servers::Abstract::getServerHumanName()
=cut
sub getServerHumanName
{
my ( $self ) = @_;
sprintf( 'Bind %s', $self->getServerVersion());
}
=item getServerVersion( )
See iMSCP::Servers::Abstract::getServerVersion()
=cut
sub getServerVersion
{
my ( $self ) = @_;
$self->{'config'}->{'NAMED_VERSION'};
}
=item addDomain( \%moduleData )
See iMSCP::Servers::Named::addDomain()
=cut
sub addDomain
{
my ( $self, $moduleData ) = @_;
# Never process the same zone twice
# Occurs only in few contexts (eg. when using BASE_SERVER_VHOST as customer domain)
return if $self->{'seen_zones'}->{$moduleData->{'DOMAIN_NAME'}};
$self->{'eventManager'}->trigger( 'beforeBindAddDomain', $moduleData );
$self->_addDmnConfig( $moduleData );
$self->_addDmnDb( $moduleData ) if $self->{'config'}->{'NAMED_MODE'} eq 'master';
$self->{'seen_zones'}->{$moduleData->{'DOMAIN_NAME'}} ||= TRUE;
$self->{'eventManager'}->trigger( 'afterBindAddDomain', $moduleData );
}
=item postaddDomain( \%moduleData )
See iMSCP::Servers::Named::postaddDomain()
=cut
sub postaddDomain
{
my ( $self, $moduleData ) = @_;
$self->{'eventManager'}->trigger( 'beforeBindPostAddDomain', $moduleData );
if ( $::imscpConfig{'CLIENT_DOMAIN_ALT_URLS'} eq 'yes' && $self->{'config'}->{'NAMED_MODE'} eq 'master' && defined $moduleData->{'ALIAS'} ) {
$self->addSubdomain( {
REAL_PARENT_DOMAIN_NAME => $moduleData->{'PARENT_DOMAIN_NAME'},
PARENT_DOMAIN_NAME => $::imscpConfig{'BASE_SERVER_VHOST'},
DOMAIN_NAME => $moduleData->{'ALIAS'} . '.' . $::imscpConfig{'BASE_SERVER_VHOST'},
EXTERNAL_MAIL => FALSE,
DOMAIN_IP => $moduleData->{'BASE_SERVER_PUBLIC_IP'},
DOMAIN_TYPE => 'sub',
BASE_SERVER_PUBLIC_IP => $moduleData->{'BASE_SERVER_PUBLIC_IP'},
OPTIONAL_ENTRIES => FALSE,
STATUS => $moduleData->{'STATUS'},
IS_ALT_URL_RECORD => TRUE
} );
}
$self->{'reload'} ||= TRUE;
$self->{'eventManager'}->trigger( 'afterBindPostAddDomain', $moduleData );
}
=item disableDomain( \%moduleData )
See iMSCP::Servers::Named::disableDomain()
When a domain is being disabled, we must ensure that the DNS data are still
present for it (eg: when doing a full upgrade or reconfiguration). This
explain here why we are executing the addDomain() method.
=cut
sub disableDomain
{
my ( $self, $moduleData ) = @_;
$self->{'eventManager'}->trigger( 'beforeBindDisableDomain', $moduleData );
$self->addDomain( $moduleData );
$self->{'eventManager'}->trigger( 'afterBindDisableDomain', $moduleData );
}
=item postdisableDomain( \%moduleData )
See iMSCP::Servers::Named::postdisableDomain()
See the ::disableDomain() method for explaination.
=cut
sub postdisableDomain
{
my ( $self, $moduleData ) = @_;
$self->{'eventManager'}->trigger( 'beforeBindPostDisableDomain', $moduleData );
$self->postaddDomain( $moduleData );
$self->{'eventManager'}->trigger( 'afterBindPostDisableDomain', $moduleData );
}
=item deleteDomain( \%moduleData )
See iMSCP::Servers::Named::deleteDomain()
=cut
sub deleteDomain
{
my ( $self, $moduleData ) = @_;
return if $moduleData->{'PARENT_DOMAIN_NAME'} eq $::imscpConfig{'BASE_SERVER_VHOST'} && !$moduleData->{'FORCE_DELETION'};
$self->{'eventManager'}->trigger( 'beforeBindDeleteDomain', $moduleData );
$self->_deleteDmnConfig( $moduleData );
if ( $self->{'config'}->{'NAMED_MODE'} eq 'master' ) {
for my $file ( "$self->{'wrkDir'}/$moduleData->{'DOMAIN_NAME'}.db",
"$self->{'config'}->{'NAMED_DB_MASTER_DIR'}/$moduleData->{'DOMAIN_NAME'}.db"
) {
iMSCP::File->new( filename => $file )->remove();
}
}
$self->{'eventManager'}->trigger( 'afterBindDeleteDomain', $moduleData );
}
=item postdeleteDomain( \%moduleData )
See iMSCP::Servers::Named::postdeleteDomain()
=cut
sub postdeleteDomain
{
my ( $self, $moduleData ) = @_;
return if $moduleData->{'PARENT_DOMAIN_NAME'} eq $::imscpConfig{'BASE_SERVER_VHOST'} && !$moduleData->{'FORCE_DELETION'};
$self->{'eventManager'}->trigger( 'beforeBindPostDeleteDomain', $moduleData );
if ( $::imscpConfig{'CLIENT_DOMAIN_ALT_URLS'} eq 'yes' && $self->{'config'}->{'NAMED_MODE'} eq 'master' && defined $moduleData->{'ALIAS'} ) {
$self->deleteSubdomain( {
PARENT_DOMAIN_NAME => $::imscpConfig{'BASE_SERVER_VHOST'},
DOMAIN_NAME => $moduleData->{'ALIAS'} . '.' . $::imscpConfig{'BASE_SERVER_VHOST'}
} );
}
$self->{'reload'} ||= TRUE;
$self->{'eventManager'}->trigger( 'afterBindPostDeleteDomain', $moduleData );
}
=item addSubdomain( \%moduleData )
See iMSCP::Servers::Named::addSubdomain()
=cut
sub addSubdomain
{
my ( $self, $moduleData ) = @_;
return unless $self->{'config'}->{'NAMED_MODE'} eq 'master';
my $wrkDbFile = iMSCP::File->new( filename => "$self->{'wrkDir'}/$moduleData->{'PARENT_DOMAIN_NAME'}.db" );
my $wrkDbFileCref = $wrkDbFile->getAsRef();
$self->{'eventManager'}->trigger( 'onLoadTemplate', lc $self->getServerName(), 'db_sub.tpl', \my $subEntry, $moduleData );
$subEntry = iMSCP::File->new( filename => "$self->{'tplDir'}/db_sub.tpl" )->get() unless defined $subEntry;
unless ( exists $self->{'serials'}->{$moduleData->{'PARENT_DOMAIN_NAME'}} ) {
$self->_updateSOAserialNumber( $moduleData->{'PARENT_DOMAIN_NAME'}, $wrkDbFileCref, $wrkDbFileCref );
}
$self->{'eventManager'}->trigger( 'beforeBindAddSubdomain', $wrkDbFileCref, \$subEntry, $moduleData );
my $net = iMSCP::Net->getInstance();
# Prepare mail entries
# FIXME: Should we remove historical smtp, relay, imap, pop... records? See:
# https://i-mscp.net/index.php/Thread/18893-Setup-SSL-Let-s-Encrypt-on-mail-client-with-customer-subdomain/?postID=58676#post58676
processBlocByRef( \$subEntry, '; sub MAIL entry BEGIN.', '; sub MAIL entry ENDING.', {
BASE_SERVER_IP_TYPE => $net->getAddrVersion( $moduleData->{'BASE_SERVER_PUBLIC_IP'} ) eq 'ipv4' ? 'A' : 'AAAA',
BASE_SERVER_IP => $moduleData->{'BASE_SERVER_PUBLIC_IP'},
DOMAIN_NAME => $moduleData->{'PARENT_DOMAIN_NAME'}
} );
# Remove optional entries if needed (in any case bloc tags are removed).
processBlocByRef( \$subEntry, '; sub OPTIONAL entries BEGIN.', '; sub OPTIONAL entries ENDING.', '', FALSE, $moduleData->{'OPTIONAL_ENTRIES'} );
my $domainIP = $net->isRoutableAddr( $moduleData->{'DOMAIN_IP'} ) ? $moduleData->{'DOMAIN_IP'} : $moduleData->{'BASE_SERVER_PUBLIC_IP'};
# Prepare subdomain entries
processVarsByRef( \$subEntry, {
SUBDOMAIN_NAME => $moduleData->{'DOMAIN_NAME'},
IP_TYPE => $net->getAddrVersion( $domainIP ) eq 'ipv4' ? 'A' : 'AAAA',
DOMAIN_IP => $domainIP
} );
# Remove previous entry if any
processBlocByRef( $wrkDbFileCref, "; sub [$moduleData->{'DOMAIN_NAME'}] entry BEGIN.", "; sub [$moduleData->{'DOMAIN_NAME'}] entry ENDING." );
# Add new entries in DNS zone file
processBlocByRef( $wrkDbFileCref, '; sub [{SUBDOMAIN_NAME}] entry BEGIN.', '; sub [{SUBDOMAIN_NAME}] entry ENDING.', $subEntry, TRUE );
$self->{'eventManager'}->trigger( 'afterBindAddSubdomain', $wrkDbFileCref, $moduleData );
$wrkDbFile->save();
$self->_compileZone( $moduleData->{'PARENT_DOMAIN_NAME'}, $wrkDbFile->{'filename'} );
}
=item postaddSubdomain( \%moduleData )
See iMSCP::Servers::Named::postaddSubdomain()
=cut
sub postaddSubdomain
{
my ( $self, $moduleData ) = @_;
$self->{'eventManager'}->trigger( 'beforeBindPostAddSubdomain', $moduleData );
if ( $::imscpConfig{'CLIENT_DOMAIN_ALT_URLS'} eq 'yes' && $self->{'config'}->{'NAMED_MODE'} eq 'master' && defined $moduleData->{'ALIAS'} ) {
$self->addSubdomain( {
REAL_PARENT_DOMAIN_NAME => $moduleData->{'PARENT_DOMAIN_NAME'},
PARENT_DOMAIN_NAME => $::imscpConfig{'BASE_SERVER_VHOST'},
DOMAIN_NAME => $moduleData->{'ALIAS'} . '.' . $::imscpConfig{'BASE_SERVER_VHOST'},
EXTERNAL_MAIL => FALSE,
DOMAIN_IP => $moduleData->{'BASE_SERVER_PUBLIC_IP'},
DOMAIN_TYPE => 'sub',
BASE_SERVER_PUBLIC_IP => $moduleData->{'BASE_SERVER_PUBLIC_IP'},
OPTIONAL_ENTRIES => FALSE,
STATUS => $moduleData->{'STATUS'},
IS_ALT_URL_RECORD => TRUE
} );
}
$self->{'reload'} ||= TRUE;
$self->{'eventManager'}->trigger( 'afterBindPostAddSubdomain', $moduleData );
}
=item disableSubdomain( \%moduleData )
See iMSCP::Servers::Named::disableSubdomain()
When a subdomain is being disabled, we must ensure that the DNS data are still present for it (eg: when doing a full
upgrade or reconfiguration). This explain here why we are executing the addSubdomain() action.
=cut
sub disableSubdomain
{
my ( $self, $moduleData ) = @_;
$self->{'eventManager'}->trigger( 'beforeBindDisableSubdomain', $moduleData );
$self->addSubdomain( $moduleData );
$self->{'eventManager'}->trigger( 'afterBindDisableSubdomain', $moduleData );
}
=item postdisableSubdomain( \%moduleData )
See iMSCP::Servers::Named::postdisableSubdomain()
See the ::disableSubdomain( ) method for explaination.
=cut
sub postdisableSubdomain
{
my ( $self, $moduleData ) = @_;
$self->{'eventManager'}->trigger( 'beforeBindPostDisableSubdomain', $moduleData );
$self->postaddSubdomain( $moduleData );
$self->{'eventManager'}->trigger( 'afterBindPostDisableSubdomain', $moduleData );
}
=item deleteSubdomain( \%moduleData )
See iMSCP::Servers::Named::deleteSubdomain()
=cut
sub deleteSubdomain
{
my ( $self, $moduleData ) = @_;
return unless $self->{'config'}->{'NAMED_MODE'} eq 'master';
my $wrkDbFile = "$self->{'wrkDir'}/$moduleData->{'PARENT_DOMAIN_NAME'}.db";
-f $wrkDbFile or die( sprintf( 'File %s not found. Run imscp-reconfigure script.', $wrkDbFile ));
$wrkDbFile = iMSCP::File->new( filename => $wrkDbFile );
my $wrkDbFileCref = $wrkDbFile->getAsRef();
unless ( exists $self->{'serials'}->{$moduleData->{'PARENT_DOMAIN_NAME'}} ) {
$self->_updateSOAserialNumber( $moduleData->{'PARENT_DOMAIN_NAME'}, $wrkDbFileCref, $wrkDbFileCref );
}
$self->{'eventManager'}->trigger( 'beforeBindDeleteSubdomain', $wrkDbFileCref, $moduleData );
processBlocByRef( $wrkDbFileCref, "; sub [$moduleData->{'DOMAIN_NAME'}] entry BEGIN.", "; sub [$moduleData->{'DOMAIN_NAME'}] entry ENDING." );
$self->{'eventManager'}->trigger( 'afterBindDeleteSubdomain', $wrkDbFileCref, $moduleData );
$wrkDbFile->save();
$self->_compileZone( $moduleData->{'PARENT_DOMAIN_NAME'}, $wrkDbFile->{'filename'} );
}
=item postdeleteSubdomain( \%moduleData )
See iMSCP::Servers::Named::postdeleteSubdomain()
=cut
sub postdeleteSubdomain
{
my ( $self, $moduleData ) = @_;
$self->{'eventManager'}->trigger( 'beforeBindPostDeleteSubdomain', $moduleData );
if ( $::imscpConfig{'CLIENT_DOMAIN_ALT_URLS'} eq 'yes' && $self->{'config'}->{'NAMED_MODE'} eq 'master' && defined $moduleData->{'ALIAS'} ) {
$self->deleteSubdomain( {
PARENT_DOMAIN_NAME => $::imscpConfig{'BASE_SERVER_VHOST'},
DOMAIN_NAME => $moduleData->{'ALIAS'} . '.' . $::imscpConfig{'BASE_SERVER_VHOST'}
} );
}
$self->{'reload'} ||= TRUE;
$self->{'eventManager'}->trigger( 'afterBindPostDeleteSubdomain', $moduleData );
}
=item addCustomDNS( \%moduleData )
See iMSCP::Servers::Named::addCustomDNS()
=cut
sub addCustomDNS
{
my ( $self, $moduleData ) = @_;
return unless $self->{'config'}->{'NAMED_MODE'} eq 'master';
my $wrkDbFile = "$self->{'wrkDir'}/$moduleData->{'DOMAIN_NAME'}.db";
-f $wrkDbFile or die( sprintf( 'File %s not found. Please rerun the imscp-reconfigure script.', $wrkDbFile ));
$wrkDbFile = iMSCP::File->new( filename => $wrkDbFile );
my $wrkDbFileCref = $wrkDbFile->getAsRef();
unless ( $self->{'serials'}->{$moduleData->{'DOMAIN_NAME'}} ) {
$self->_updateSOAserialNumber( $moduleData->{'DOMAIN_NAME'}, $wrkDbFileCref, $wrkDbFileCref );
}
$self->{'eventManager'}->trigger( 'beforeBindAddCustomDNS', $wrkDbFileCref, $moduleData );
my @customDNS = ();
push @customDNS, join "\t", @{ $_ } for @{ $moduleData->{'DNS_RECORDS'} };
open my $fh, '<', $wrkDbFileCref or die( sprintf( "Couldn't open in-memory file handle: %s", $! ));
my ( $newWrkDbFileC, $origin ) = ( '', '' );
while ( my $line = <$fh> ) {
my $isOrigin = $line =~ /^\$ORIGIN\s+([^\s;]+).*\n$/;
$origin = $1 if $isOrigin; # Update $ORIGIN if needed
unless ( $isOrigin || index( $line, '$' ) == 0 || index( $line, ';' ) == 0 ) {
# Process $ORIGIN substitutions
$line =~ s/\@/$origin/g;
$line =~ s/^(\S+?[^\s.])\s+/$1.$origin\t/;
# Skip default SPF record line if SPF record for the same DNS name exists in @customDNS
next if $line =~ /^(\S+)\s+.*?\s+"v=\bspf1\b.*?"/ && grep /^\Q$1\E\s+.*?\s+"v=\bspf1\b.*?"/, @customDNS;
}
$newWrkDbFileC .= $line;
}
close( $fh );
undef $wrkDbFileCref;
processBlocByRef( \$newWrkDbFileC, '; custom DNS entries BEGIN.', '; custom DNS entries ENDING.', <<"EOF" );
; custom DNS entries BEGIN.
@{[ join "\n", @customDNS ]}
; custom DNS entries ENDING.
EOF
$self->{'eventManager'}->trigger( 'afterBindAddCustomDNS', \$newWrkDbFileC, $moduleData );
$wrkDbFile->set( $newWrkDbFileC )->save();
$self->_compileZone( $moduleData->{'DOMAIN_NAME'}, $wrkDbFile->{'filename'} );
$self->{'reload'} ||= TRUE;
}
=back
=head1 PRIVATE METHODS
=over 4
=item _init( )
Initialize instance
See iMSCP::Servers::Named::_init()
=cut
sub _init
{
my ( $self ) = @_;
ref $self ne __PACKAGE__ or croak( sprintf( 'The %s class is an abstract class which cannot be instantiated', __PACKAGE__ ));
@{ $self }{qw/ restart reload serials seen_zones cfgDir /} = ( FALSE, FALSE, {}, {}, "$::imscpConfig{'CONF_DIR'}/bind" );
@{ $self }{qw/ bkpDir wrkDir tplDir /} = ( "$self->{'cfgDir'}/backup", "$self->{'cfgDir'}/working", "$self->{'cfgDir'}/parts" );
$self->SUPER::_init();
}
=item _setVersion( )
Set Bind version
Return void, die on failure
=cut
sub _setVersion
{
my ( $self ) = @_;
die( sprintf( 'The %s class must implement the _setVersion() method', ref $self ));
}
=item _addDmnConfig( \%moduleData )
Add domain DNS configuration
Param hashref \%moduleData Data as provided by the Domain|SubAlias modules
Return void, die on failure
=cut
sub _addDmnConfig
{
my ( $self, $moduleData ) = @_;
my ( $cfgFileName, $cfgFileDir ) = fileparse( $self->{'config'}->{'NAMED_LOCAL_CONF_FILE'} || $self->{'config'}->{'NAMED_CONF_FILE'} );
my $cfgFile = iMSCP::File->new( filename => "$self->{'wrkDir'}/$cfgFileName" );
my $cfgWrkFileCref = $cfgFile->getAsRef();
my $tplFileName = "cfg_$self->{'config'}->{'NAMED_MODE'}.tpl";
$self->{'eventManager'}->trigger( 'onLoadTemplate', lc $self->getServerName(), $tplFileName, \my $tplCfgEntryC, $moduleData );
$tplCfgEntryC = iMSCP::File->new( filename => "$self->{'tplDir'}/$tplFileName" )->get() unless defined $tplCfgEntryC;
$self->{'eventManager'}->trigger( 'beforeBindAddDmnConfig', $cfgWrkFileCref, \$tplCfgEntryC, $moduleData );
chomp( $tplCfgEntryC );
my $vars = {
NAMED_DB_FORMAT => $self->{'config'}->{'NAMED_DB_FORMAT'} =~ s/=\d//r,
DOMAIN_NAME => $moduleData->{'DOMAIN_NAME'}
};
if ( $self->{'config'}->{'NAMED_MODE'} eq 'master' ) {
if ( $self->{'config'}->{'NAMED_SECONDARY_DNS'} ne 'no' ) {
$vars->{'NAMED_SECONDARY_DNS'} = join( '; ', split( ';', $self->{'config'}->{'NAMED_SECONDARY_DNS'} )) . '; localhost;';
} else {
$vars->{'NAMED_SECONDARY_DNS'} = 'localhost;';
}
} else {
$vars->{'NAMED_PRIMARY_DNS'} = join( '; ', split( ';', $self->{'config'}->{'NAMED_PRIMARY_DNS'} )) . ';';
}
# Remove previous entry if any
processBlocByRef(
$cfgWrkFileCref, "// imscp [$moduleData->{'DOMAIN_NAME'}] entry BEGIN.", "// imscp [$moduleData->{'DOMAIN_NAME'}] entry ENDING."
);
# Add new entry
processBlocByRef( $cfgWrkFileCref, '// imscp [{ENTRY_ID}] entry BEGIN.', '// imscp [{ENTRY_ID}] entry ENDING.', <<"EOF", TRUE );
// imscp [$moduleData->{'DOMAIN_NAME'}] entry BEGIN.
@{ [ processVars( $tplCfgEntryC, $vars ) ] }
// imscp [$moduleData->{'DOMAIN_NAME'}] entry ENDING.
EOF
$self->{'eventManager'}->trigger( 'afterBindAddDmnConfig', $cfgWrkFileCref, $moduleData );
$cfgFile->save()->owner( $::imscpConfig{'ROOT_USER'}, $self->{'config'}->{'NAMED_GROUP'} )->mode( 0640 )->copy( $cfgFileDir . $cfgFileName, {
preserve => TRUE
} );
}
=item _deleteDmnConfig( \%moduleData )
Delete domain DNS configuration
Param hashref \%moduleData Data as provided by the Domain|SubAlias modules
Return void, die on failure
=cut
sub _deleteDmnConfig
{
my ( $self, $moduleData ) = @_;
my ( $cfgFileName, $cfgFileDir ) = fileparse( $self->{'config'}->{'NAMED_LOCAL_CONF_FILE'} || $self->{'config'}->{'NAMED_CONF_FILE'} );
my $cfgFile = iMSCP::File->new( filename => "$self->{'wrkDir'}/$cfgFileName" );
my $cfgWrkFileCref = $cfgFile->getAsRef();
$self->{'eventManager'}->trigger( 'beforeBindDeleteDomainConfig', $cfgWrkFileCref, $moduleData );
processBlocByRef( $cfgWrkFileCref, "// imscp [$moduleData->{'DOMAIN_NAME'}] entry BEGIN.", "// imscp [$moduleData->{'DOMAIN_NAME'}] entry ENDING." );
$self->{'eventManager'}->trigger( 'afterBindDeleteDomainConfig', $cfgWrkFileCref, $moduleData );
$cfgFile->save()->owner( $::imscpConfig{'ROOT_USER'}, $self->{'config'}->{'NAMED_GROUP'} )->mode( 0640 )->copy(
$cfgFileDir . $cfgFileName, { preserve => TRUE }
);
}
=item _addDmnDb( \%moduleData )
Add domain DNS zone file
Param hashref \%moduleData Data as provided by the Domain|SubAlias modules
Return void, die on failure
=cut
sub _addDmnDb
{
my ( $self, $moduleData ) = @_;
my $wrkDbFile = iMSCP::File->new( filename => "$self->{'wrkDir'}/$moduleData->{'DOMAIN_NAME'}.db" );
my $wrkDbFileC = -f $wrkDbFile ? $wrkDbFile->get() : undef;
$self->{'eventManager'}->trigger( 'onLoadTemplate', lc $self->getServerName(), 'db.tpl', \my $tplDbFileC, $moduleData );
$tplDbFileC = iMSCP::File->new( filename => "$self->{'tplDir'}/db.tpl" )->get() unless defined $tplDbFileC;
$self->_updateSOAserialNumber( $moduleData->{'DOMAIN_NAME'}, \$tplDbFileC, \$wrkDbFileC );
$self->{'eventManager'}->trigger( 'beforeBindAddDomainDb', \$tplDbFileC, $moduleData );
my $nsRecordB = getBlocByRef( \$tplDbFileC, '; dmn NS RECORD entry BEGIN.', '; dmn NS RECORD entry ENDING.' );
my $glueRecordB = getBlocByRef( \$tplDbFileC, '; dmn NS GLUE RECORD entry BEGIN.', '; dmn NS GLUE RECORD entry ENDING.' );
my $net = iMSCP::Net->getInstance();
my $domainIP = $net->isRoutableAddr( $moduleData->{'DOMAIN_IP'} ) ? $moduleData->{'DOMAIN_IP'} : $moduleData->{'BASE_SERVER_PUBLIC_IP'};
if ( length $nsRecordB || length $glueRecordB ) {
my @nsIPs = ( $domainIP, ( ( $self->{'config'}->{'NAMED_SECONDARY_DNS'} eq 'no' )
? () : split ';', $self->{'config'}->{'NAMED_SECONDARY_DNS'} )
);
my ( $nsRecords, $glueRecords ) = ( '', '' );
for my $ipAddrType ( qw/ ipv4 ipv6 / ) {
my $nsNumber = 1;
for my $ipAddr ( @nsIPs ) {
next unless $net->getAddrVersion( $ipAddr ) eq $ipAddrType;
$nsRecords .= processVars( $nsRecordB, { NS_NAME => 'ns' . $nsNumber } ) if length $nsRecordB;
$glueRecords .= processVars( $glueRecordB, {
NS_NAME => 'ns' . $nsNumber,
NS_IP_TYPE => $ipAddrType eq 'ipv4' ? 'A' : 'AAAA',
NS_IP => $ipAddr
} ) if length $glueRecordB;
$nsNumber++;
}
}
processBlocByRef( \$tplDbFileC, '; dmn NS RECORD entry BEGIN.', '; dmn NS RECORD entry ENDING.', $nsRecords ) if length $nsRecordB;
if ( length $glueRecordB ) {
processBlocByRef( \$tplDbFileC, '; dmn NS GLUE RECORD entry BEGIN.', '; dmn NS GLUE RECORD entry ENDING.', $glueRecords );
}
}
# Prepare mail entries
# FIXME: Should we remove historical smtp, relay, imap, pop... records? See:
# https://i-mscp.net/index.php/Thread/18893-Setup-SSL-Let-s-Encrypt-on-mail-client-with-customer-subdomain/?postID=58676#post58676
processBlocByRef( \$tplDbFileC, '; dmn MAIL entry BEGIN.', '; dmn MAIL entry ENDING.', {
BASE_SERVER_IP_TYPE => $net->getAddrVersion( $moduleData->{'BASE_SERVER_PUBLIC_IP'} ) eq 'ipv4' ? 'A' : 'AAAA',
BASE_SERVER_IP => $moduleData->{'BASE_SERVER_PUBLIC_IP'}
} );
# Prepare domain entries
processVarsByRef( \$tplDbFileC, {
DOMAIN_NAME => $moduleData->{'DOMAIN_NAME'},
IP_TYPE => $net->getAddrVersion( $domainIP ) eq 'ipv4' ? 'A' : 'AAAA',
DOMAIN_IP => $domainIP
} );
unless ( !defined $wrkDbFileC || iMSCP::Getopt->context() eq 'installer' ) {
# Re-add subdomain entries
processBlocByRef( \$tplDbFileC, '; sub entries BEGIN.', '; sub entries ENDING.', getBlocByRef(
\$wrkDbFileC, '; sub entries BEGIN.', '; sub entries ENDING.', TRUE
));
# Re-add custom DNS entries
processBlocByRef( \$tplDbFileC, '; custom DNS entries BEGIN.', '; custom DNS entries ENDING.', getBlocByRef(
\$wrkDbFileC, '; custom DNS entries BEGIN.', '; custom DNS entries ENDING.', TRUE
));
}
$self->{'eventManager'}->trigger( 'afterBindAddDomainDb', \$tplDbFileC, $moduleData );
$wrkDbFile->set( $tplDbFileC )->save();
$self->_compileZone( $moduleData->{'DOMAIN_NAME'}, $wrkDbFile->{'filename'} );
}
=item _updateSOAserialNumber( $zone, \$zoneFileC, \$oldZoneFileC )
Update SOA serial number for the given zone
Note: Format follows RFC 1912 section 2.2 recommendations.
Param string zone Zone name
Param scalarref \$zoneFileC Reference to zone file content
Param scalarref \$oldZoneFileC Reference to old zone file content
Return void, die on failure
=cut
sub _updateSOAserialNumber
{
my ( $self, $zone, $zoneFileC, $oldZoneFileC ) = @_;
$oldZoneFileC = $zoneFileC unless defined ${ $oldZoneFileC };
${ $oldZoneFileC } =~ /^\s+(?:(?<date>\d{8})(?<nn>\d{2})|(?<placeholder>\{TIMESTAMP\}))\s*;[^\n]*\n/m or die(
sprintf( "Couldn't update SOA serial number for the %s DNS zone: Serial data not found", $zone )
);
my %rc = %+;
my ( $d, $m, $y ) = ( gmtime() )[3 .. 5];
my $nowDate = sprintf( "%d%02d%02d", $y+1900, $m+1, $d );
if ( exists $+{'placeholder'} ) {
$self->{'serials'}->{$zone} = $nowDate . '00';
processVarsByRef( $zoneFileC, {
TIMESTAMP => $self->{'serials'}->{$zone}
} );
return;
}
if ( $rc{'date'} >= $nowDate ) {
$rc{'nn'}++;
if ( $rc{'nn'} >= 99 ) {
$rc{'date'}++;
$rc{'nn'} = '00';
}
} else {
$rc{'date'} = $nowDate;
$rc{'nn'} = '00';
}
$self->{'serials'}->{$zone} = $rc{'date'} . $rc{'nn'};
${ $zoneFileC } =~ s/^(\s+)(?:\d{10}|\{TIMESTAMP\})(\s*;[^\n]*\n)/$1$self->{'serials'}->{$zone}$2/m;
}
=item _compileZone( $zonename, $filename )
Compiles the given zone
Param string $zonename Zone name
Param string $filename Path to zone filename (zone in text format)
Return void, die on failure
=cut
sub _compileZone