root/tags/omega-1.0.0-rc3/engine/ispcp-dmn-mngr

Revision 976, 105.7 kB (checked in by rats, 10 months ago)

* Fixed #965: Error in ispcp-dmn-mngr
* Fixed #958: Some Errors in Updatescript

Line 
1 #!/usr/bin/perl
2
3 # ispCP ω (OMEGA) a Virtual Hosting Control Panel
4 # Copyright (c) 2001-2006 by moleSoftware GmbH
5 # http://www.molesoftware.com
6 # Copyright (c) 2006-2007 by isp Control Panel
7 # http://isp-control.net
8 #
9 #
10 # License:
11 #    This program is free software; you can redistribute it and/or
12 #    modify it under the terms of the MPL Mozilla Public License
13 #    as published by the Free Software Foundation; either version 1.1
14 #    of the License, or (at your option) any later version.
15 #
16 #    This program is distributed in the hope that it will be useful,
17 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
18 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 #    MPL Mozilla Public License for more details.
20 #
21 #    You may have received a copy of the MPL Mozilla Public License
22 #    along with this program.
23 #
24 #    An on-line copy of the MPL Mozilla Public License can be found
25 #    http://www.mozilla.org/MPL/MPL-1.1.html
26 #
27 #
28 # The ISPCP ω Home Page is at:
29 #
30 #    http://isp-control.net
31 #
32
33 use FindBin;
34 use lib "$FindBin::Bin/";
35 require 'ispcp_common_code.pl';
36
37 use strict;
38
39 use warnings;
40
41 use Net::LibIDN ':all';
42
43 $main::dmn_task_id = undef;
44
45 ################################################################################
46 ##                                SUBROUTINES                                 ##
47 ################################################################################
48 ##
49 ## Start Up!
50 ##
51
52 sub dmn_mngr_start_up {
53
54     my ($rs, $rdata) = (undef, undef);
55
56     push_el(\@main::el, 'dmn_mngr_start_up()', 'Starting...');
57
58     # checking for master process;
59
60     $rs = check_master();
61
62     return $rs if ($rs != 0);
63
64     # Let's clear Execution Logs, if any.
65
66     if (-e $main::ispcp_dmn_mngr_el) {
67
68         $rs = del_file($main::ispcp_dmn_mngr_el);
69
70         return $rs if ($rs != 0);
71
72     }
73
74     # config check;
75
76     $rs = get_conf();
77
78     return $rs if ($rs != 0);
79
80     # sql check;
81
82     #
83     # getting initial data also must be done here;
84     #
85
86     my $sql = "select admin_id from admin where admin_id = 1;";
87
88     ($rs, $rdata) = doSQL($sql);
89
90     return $rs if ($rs != 0);
91
92     #
93     # getting task id and domain record id;
94     #
95
96     $main::dmn_task_id = $ARGV[0];
97
98     push_el(\@main::el, 'dmn_mngr_start_up()', 'Ending...');
99
100     return 0;
101
102 }
103
104 ################################################################################
105 ##
106 ## Shut Down!
107 ##
108
109 sub dmn_mngr_shut_down {
110
111     my $rs = undef;
112
113     push_el(\@main::el, 'dmn_mngr_shut_down()', 'Starting...');
114
115     push_el(\@main::el, 'dmn_mngr_shut_down()', 'Ending...');
116
117     return 0;
118
119 }
120
121 ###################################### DNS #####################################
122
123 ################################################################################
124 ##
125 ## Add DNS CFG Data
126 ##
127
128 sub dmn_add_named_cfg_data {
129
130     my ($dmn_data) = @_;
131
132     my ($rs, $rdata) = (undef, undef);
133
134     push_el(\@main::el, 'dmn_add_named_cfg_data()', 'Starting...');
135
136     if (!defined($dmn_data) || $dmn_data eq '') {
137
138         push_el(\@main::el, 'dmn_add_named_cfg_data()', 'ERROR: Undefined Input Data...');
139
140         return -1;
141
142     }
143
144     #
145     # Initial data we need;
146     #
147
148     my $dmn_name = @$dmn_data[1];
149
150     my $conf_dir = $main::cfg{'CONF_DIR'};
151
152     my $sys_cfg = $main::cfg{'BIND_CONF_FILE'};
153
154     my $named_db_dir = $main::cfg{'BIND_DB_DIR'};
155
156
157     my $tpl_dir = "$conf_dir/bind/parts";
158
159     my $backup_dir = "$conf_dir/bind/backup";
160
161     my $working_dir = "$conf_dir/bind/working";
162
163
164     my $timestamp = time;
165
166     my $backup_cfg = "$backup_dir/named.conf.$timestamp";
167
168     my $working_cfg = "$working_dir/named.conf";
169
170     #
171     #  BEGIN/END tags, and templates needed for this config;
172     #
173
174     my ($dta_b, $dta_e, $entry_b, $entry_e, $entry) = ('', '', '', '', '');
175
176     (
177      $rs,
178      $dta_b,
179      $dta_e,
180      $entry_b,
181      $entry_e,
182      $entry
183     ) = get_tpl(
184                 $tpl_dir,
185                 'cfg_dta_b.tpl',
186                 'cfg_dta_e.tpl',
187                 'cfg_entry_b.tpl',
188                 'cfg_entry_e.tpl',
189                 'cfg_entry.tpl'
190                );
191
192     return $rs if ($rs != 0);
193
194     #
195     # Let's construct nedded tags and entries;
196     #
197
198     my %tag_hash = (
199                     '{DMN_NAME}' => $dmn_name,
200                     '{DB_DIR}' => $named_db_dir
201                    );
202
203     my ($entry_b_val, $entry_e_val, $entry_val) = ('', '', '');
204
205     (
206      $rs,
207      $entry_b_val,
208      $entry_e_val,
209      $entry_val
210     ) = prep_tpl(
211                  \%tag_hash,
212                  $entry_b,
213                  $entry_e,
214                  $entry
215                 );
216
217     return $rs if ($rs != 0);
218
219     #
220     # Let's get Sytem and Workind config files;
221     #
222
223     my ($sys, $working) = ('', '');
224
225
226     ($rs, $sys) = get_file($sys_cfg);
227
228     return $rs  if ($rs != 0);
229
230     ($rs, $working) = get_file($working_cfg);
231
232     return $rs  if ($rs != 0);
233
234     ($rs, $rdata) = get_tag($dta_b, $dta_e, $working);
235
236     return $rs if ($rs != 0);
237
238     #
239     # Is the new domain entry exists ?
240     #
241
242     ($rs, $rdata) = get_tag($entry_b_val, $entry_e_val, $working);
243
244     if ($rs == 0) {
245
246         # Yes it exists ! Then we must delete it !
247
248         ($rs, $working) = del_tag($entry_b_val, "$entry_e_val\n", $working);
249
250         return $rs if ($rs != 0);
251
252     }
253
254     ($rs, $rdata) = get_tag($entry_b, $entry_e, $working);
255
256     return $rs if ($rs != 0);
257
258     #
259     # Let's contruct the replacement and do it;
260     #
261
262     my $entry_repl = "$entry_b_val$entry_val$entry_e_val\n$entry_b$entry_e";
263
264     ($rs, $working) = repl_tag($entry_b, $entry_e, $working, $entry_repl);
265
266     return $rs if ($rs != 0);
267
268     #
269     # Here we'll backup production config file;
270     #
271
272     $rs = sys_command("$main::cfg{'CMD_CP'} -p $sys_cfg $backup_cfg");
273
274     return $rs if ($rs != 0);
275
276     #
277     # Let's save working copy;
278     #
279
280     $rs = store_file($working_cfg, $working, 'root', 'root', 0644);
281
282     return $rs if ($rs != 0);
283
284     #
285     # Here we'll replace data in production config file with data in working
286     # confing file. A little workaround will be done. If working copy data does not exist
287     # in production config then we will add it;
288     #
289
290     ($rs, $rdata) = get_tag($dta_b, $dta_e, $sys);
291
292     if ($rs == 0) { # YES ! Data is here ! /in production config file/;
293
294         ($rs, $sys) = repl_tag($dta_b, $dta_e, $sys, $working);
295
296         return $rs if ($rs != 0);
297
298     } elsif ($rs == -5) {
299
300         $sys .= $working;
301
302     } else {
303
304         return $rs;
305
306     }
307
308     $rs = store_file($sys_cfg, $sys, 'root', 'root', 0644);
309
310     return $rs if ($rs != 0);
311
312
313     push_el(\@main::el, 'dmn_add_named_cfg_data()', 'Ending...');
314
315     return 0;
316 }
317
318 ################################################################################
319 ##
320 ## Delete DNS CFG Data
321 ##
322
323 sub dmn_del_named_cfg_data {
324
325     my ($dmn_data) = @_;
326
327     my ($rs, $rdata) = (undef, undef);
328
329     push_el(\@main::el, 'dmn_del_named_cfg_data()', 'Starting...');
330
331     if (!defined($dmn_data) || $dmn_data eq '') {
332
333         push_el(\@main::el, 'dmn_del_named_cfg_data()', 'ERROR: Undefined Input Data...');
334
335         return -1;
336
337     }
338
339     #
340     # Initial data we need;
341     #
342
343     my $dmn_name = @$dmn_data[1];
344
345     my $conf_dir = $main::cfg{'CONF_DIR'};
346
347     my $sys_cfg = $main::cfg{'BIND_CONF_FILE'};
348
349     my $named_db_dir = $main::cfg{'BIND_DB_DIR'};
350
351
352     my $tpl_dir = "$conf_dir/bind/parts";
353
354     my $backup_dir = "$conf_dir/bind/backup";
355
356     my $working_dir = "$conf_dir/bind/working";
357
358
359     my $timestamp = time;
360
361     my $backup_cfg = "$backup_dir/named.conf.$timestamp";
362
363     my $working_cfg = "$working_dir/named.conf";
364
365     #
366     #  BEGIN/END tags, and templates needed for this config;
367     #
368
369     my ($dta_b, $dta_e, $entry_b, $entry_e, $entry) = ('', '', '', '', '');
370
371     (
372      $rs,
373      $dta_b,
374      $dta_e,
375      $entry_b,
376      $entry_e,
377      $entry
378     ) = get_tpl(
379                 $tpl_dir,
380                 'cfg_dta_b.tpl',
381                 'cfg_dta_e.tpl',
382                 'cfg_entry_b.tpl',
383                 'cfg_entry_e.tpl',
384                 'cfg_entry.tpl'
385                );
386
387     return $rs if ($rs != 0);
388
389     #
390     # Let's construct nedded tags and entries;
391     #
392
393     my %tag_hash = (
394                     '{DMN_NAME}' => $dmn_name,
395                     '{DB_DIR}' => $named_db_dir
396                    );
397
398     my ($entry_b_val, $entry_e_val, $entry_val) = ('', '', '');
399
400     (
401      $rs,
402      $entry_b_val,
403      $entry_e_val,
404      $entry_val
405     ) = prep_tpl(
406                  \%tag_hash,
407                  $entry_b,
408                  $entry_e,
409                  $entry
410                 );
411
412     return $rs if ($rs != 0);
413
414     #
415     # Let's get Sytem and Workind config files;
416     #
417
418     my ($sys, $working) = ('', '');
419
420
421     ($rs, $sys) = get_file($sys_cfg);
422
423     return $rs  if ($rs != 0);
424
425     ($rs, $working) = get_file($working_cfg);
426
427     return $rs  if ($rs != 0);
428
429     ($rs, $rdata) = get_tag($dta_b, $dta_e, $working);
430
431     return $rs if ($rs != 0);
432
433     #
434     # Is the new domain entry exists ?
435     #
436
437     ($rs, $rdata) = get_tag($entry_b_val, $entry_e_val, $working);
438
439     if ($rs == 0) {
440
441         # Yes it exists ! Then we must delete it !
442
443         ($rs, $working) = del_tag($entry_b_val, "$entry_e_val\n", $working);
444
445         return $rs if ($rs != 0);
446
447     }
448
449     ($rs, $rdata) = get_tag($entry_b, $entry_e, $working);
450
451     return $rs if ($rs != 0);
452
453     #
454     # Let's contruct the replacement and do it;
455     #
456     #
457     #my $entry_repl = "$entry_b_val$entry_val$entry_e_val\n$entry_b$entry_e";
458     #
459     #($rs, $working) = repl_tag($entry_b, $entry_e, $working, $entry_repl);
460     #
461     #return $rs if ($rs != 0);
462     #
463
464     #
465     # Here we'll backup production config file;
466     #
467
468     $rs = sys_command("$main::cfg{'CMD_CP'} -p $sys_cfg $backup_cfg");
469
470     return $rs if ($rs != 0);
471
472     #
473     # Let's save working copy;
474     #
475
476     $rs = store_file($working_cfg, $working, 'root', 'root', 0644);
477
478     return $rs if ($rs != 0);
479
480     #
481     # Here we'll replace data in production config file with data in working
482     # confing file. A little workaround will be done. If working copy data does not exist
483     # in production config then we will add it;
484     #
485
486     ($rs, $rdata) = get_tag($dta_b, $dta_e, $sys);
487
488     if ($rs == 0) { # YES ! Data is here ! /in production config file/;
489
490         ($rs, $sys) = repl_tag($dta_b, $dta_e, $sys, $working);
491
492         return $rs if ($rs != 0);
493
494     } elsif ($rs == -5) {
495
496         $sys .= $working;
497
498     } else {
499
500         return $rs;
501
502     }
503
504     $rs = store_file($sys_cfg, $sys, 'root', 'root', 0644);
505
506     return $rs if ($rs != 0);
507
508
509     push_el(\@main::el, 'dmn_del_named_cfg_data()', 'Ending...');
510
511     return 0;
512 }
513
514 ################################################################################
515 ##
516 ## Add DNS DB Data
517 ##
518
519 sub dmn_add_named_db_data {
520
521     my ($dmn_data) = @_;
522
523     my ($rs, $rdata) = (undef, undef);
524
525     push_el(\@main::el, 'dmn_add_named_db_data()', 'Starting...');
526
527     if (!defined($dmn_data) || $dmn_data eq '') {
528
529         push_el(\@main::el, 'dmn_add_named_db_data()', 'ERROR: Undefined Input Data...');
530
531         return -1;
532
533     }
534
535     #
536     # Initial data we need;
537     #
538
539     my $dmn_name = @$dmn_data[1];
540
541     my $dmn_ip = @$dmn_data[21];
542
543
544     my $conf_dir = $main::cfg{'CONF_DIR'};
545
546     my $named_db_dir = $main::cfg{'BIND_DB_DIR'};
547
548     my $base_svr_ip = $main::cfg{'BASE_SERVER_IP'};
549
550     my $sec_dns_ip = $main::cfg{'SECONDARY_DNS'};
551
552     #
553     # Any secondary DNS defined;
554     #
555
556         if (!$sec_dns_ip) {
557                 $sec_dns_ip = $base_svr_ip;
558         }
559
560     my $tpl_dir = "$conf_dir/bind/parts";
561
562     my $backup_dir = "$conf_dir/bind/backup";
563
564     my $working_dir = "$conf_dir/bind/working";
565
566
567     my $db_fname = "$dmn_name.db";
568
569
570     my $sys_cfg = "$named_db_dir/$db_fname";
571
572     my $working_cfg = "$working_dir/$db_fname";
573
574     #
575     # Let's get needed tags and templates;
576     #
577
578     my ($entry, $dns2_b, $dns2_e) = ('', '', '');
579
580     ($rs, $entry, $dns2_b, $dns2_e) = get_tpl(
581                                               $tpl_dir,
582                                               'db_e.tpl',
583                                               'db_dns2_b.tpl',
584                                               'db_dns2_e.tpl'
585                                              );
586
587     return $rs if ($rs != 0);
588
589         #
590         # RFC 1912
591         #
592
593         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
594
595         my $time2 = sprintf "%4d%02d%02d00",$year+1900,$mon+1,$mday;
596
597     #
598     # Let's prepare them;
599     #
600
601     my %tag_hash = (
602                     '{DMN_NAME}' => $dmn_name,
603                     '{DMN_IP}' => $dmn_ip,
604                     '{BASE_SERVER_IP}' => $base_svr_ip,
605                     '{SECONDARY_DNS_IP}' => $sec_dns_ip,
606                     '{TIMESTAMP}' => $time2
607                    );
608
609     ($rs, $entry, $dns2_b, $dns2_e) = prep_tpl(
610                                                \%tag_hash,
611                                                $entry,
612                                                $dns2_b,
613                                                $dns2_e
614                                               );
615
616     return $rs if ($rs != 0);
617
618     #if ($main::cfg{'SECONDARY_DNS'}) {
619
620     #    my $dns2 = undef;
621
622     #    $dns2 = "\t\t \t NS \t $main::cfg{'SECONDARY_DNS'}.\n";
623
624     #    $dns2 = "$dns2_b$dns2$dns2_e";
625
626     #    ($rs, $entry) = repl_tag($dns2_b, $dns2_e, $entry, $dns2);
627
628     #    return $rs if ($rs != 0);
629
630     #} else {
631
632     #   ($rs, $entry) = del_tag($dns2_b, $dns2_e, $entry);
633
634     #    return $rs if ($rs != 0);
635
636     #}
637
638     #
639     # Let's store generated data;
640     #
641
642     $rs = store_file($working_cfg, $entry, 'root', 'root', 0644);
643
644     return $rs if ($rs != 0);
645
646     $rs = store_file($sys_cfg, $entry, 'root', 'root', 0644);
647
648     return $rs if ($rs != 0);
649
650     push_el(\@main::el, 'dmn_add_named_db_data()', 'Ending...');
651
652
653     return 0;
654 }
655
656 ################################################################################
657 ##
658 ## Delte DNS DB Data
659 ##
660
661 sub dmn_del_named_db_data {
662
663     my ($dmn_data) = @_;
664
665     my ($rs, $rdata) = (undef, undef);
666
667     push_el(\@main::el, 'dmn_del_named_db_data()', 'Starting...');
668
669     if (!defined($dmn_data) || $dmn_data eq '') {
670
671         push_el(\@main::el, 'dmn_del_named_db_data()', 'ERROR: Undefined Input Data...');
672
673         return -1;
674
675     }
676
677     #
678     # Initial data we need;
679     #
680
681     my $dmn_name = @$dmn_data[1];
682
683     my $dmn_ip = @$dmn_data[21];
684
685
686     my $conf_dir = $main::cfg{'CONF_DIR'};
687
688     my $named_db_dir = $main::cfg{'BIND_DB_DIR'};
689
690
691     my $tpl_dir = "$conf_dir/bind/parts";
692
693     my $backup_dir = "$conf_dir/bind/backup";
694
695     my $working_dir = "$conf_dir/bind/working";
696
697
698     my $db_fname = "$dmn_name.db";
699
700
701     my $sys_cfg = "$named_db_dir/$db_fname";
702
703     my $working_cfg = "$working_dir/$db_fname";
704
705     #
706     # Let's remove .db files for this domain;
707     #
708
709     $rs = del_file($working_cfg);
710
711     return $rs if ($rs != 0);
712
713     $rs = del_file($sys_cfg);
714
715     return $rs if ($rs != 0);
716
717     push_el(\@main::el, 'dmn_del_named_db_data()', 'Ending...');
718
719
720     return 0;
721 }
722
723 ################################################################################
724 ##
725 ## Add DNS Data
726 ##
727
728 sub dmn_add_named_data {
729
730     my ($dmn_data) = @_;
731
732     my $rs = undef;
733
734     push_el(\@main::el, 'dmn_add_named_data()', 'Starting...');
735
736     if (!defined($dmn_data) || $dmn_data eq '') {
737
738         push_el(\@main::el, 'dmn_add_named_data()', 'ERROR: Undefined Input Data...');
739
740         return -1;
741
742     }
743
744     return 0 if ($main::cfg{'CMD_NAMED'} eq 'no');
745
746     $rs = dmn_add_named_cfg_data($dmn_data);
747
748     return $rs if ($rs != 0);
749
750     $rs = dmn_add_named_db_data($dmn_data);
751
752     return $rs if ($rs != 0);
753
754     push_el(\@main::el, 'dmn_add_named_data()', 'Ending...');
755
756     return 0;
757 }
758
759 ################################################################################
760 ##
761 ## Change DNS Data
762 ##
763
764 sub dmn_change_named_data {
765
766     my ($dmn_data) = @_;
767
768     my $rs = undef;
769
770     push_el(\@main::el, 'dmn_change_named_data()', 'Starting...');
771
772     if (!defined($dmn_data) || $dmn_data eq '') {
773
774         push_el(\@main::el, 'dmn_change_named_data()', 'ERROR: Undefined Input Data...');
775
776         return -1;
777
778     }
779
780     $rs = dmn_add_named_data($dmn_data);
781
782     return $rs if ($rs != 0);
783
784     push_el(\@main::el, 'dmn_change_named_data()', 'Ending...');
785
786     return 0;
787 }
788
789 ################################################################################
790 ##
791 ## Delete DNS Data
792 ##
793
794 sub dmn_del_named_data {
795
796     my ($dmn_data) = @_;
797
798     my $rs = undef;
799
800     push_el(\@main::el, 'dmn_del_named_data()', 'Starting...');
801
802     if (!defined($dmn_data) || $dmn_data eq '') {
803
804         push_el(\@main::el, 'dmn_del_named_data()', 'ERROR: Undefined Input Data...');
805
806         return -1;
807
808     }
809
810     return 0 if ($main::cfg{'CMD_NAMED'} eq 'no');
811
812     $rs = dmn_del_named_cfg_data($dmn_data);
813
814     return $rs if ($rs != 0);
815
816     $rs = dmn_del_named_db_data($dmn_data);
817
818     return $rs if ($rs != 0);
819
820     push_el(\@main::el, 'dmn_del_named_data()', 'Ending...');
821
822     return 0;
823 }
824
825 ##################################### HTTPd ####################################
826
827 ################################################################################
828 ##
829 ## Generate Auto Number
830 ##
831
832 sub get_auto_num {
833
834     my ($rs, $num, $rdata) = (undef, undef, undef);
835
836     push_el(\@main::el, 'get_auto_num()', 'Starting...');
837
838     my $sql = "insert into auto_num (msg) values ('auto-num')";
839
840     ($rs, $rdata) = doSQL($sql);
841
842     return $rs if ($rs != 0);
843
844     $num = $main::db -> {'mysql_insertid'};
845
846     push_el(\@main::el, 'get_auto_num()', 'Ending...');
847
848     return $num;
849
850     close('db');
851
852 }
853
854 ################################################################################
855 ##
856 ## Generate HTTPd Domain Group Entry
857 ##
858
859 sub gen_httpd_dmn_group_entry {
860
861     my ($dmn_data) = @_;
862
863     push_el(\@main::el, 'gen_httpd_dmn_group_entry()', 'Starting...');
864
865     if (!defined($dmn_data) || $dmn_data eq '') {
866         push_el(\@main::el, 'gen_httpd_dmn_group_entry()', "ERORR: Undefined input data...");
867         return (-1, '');
868     }
869
870     my ($rs, $rdata) = (undef, undef);
871
872     my $dmn_id = @$dmn_data[0];
873
874     my $dmn_name = @$dmn_data[1];
875
876     my $dmn_grp = $dmn_name;
877
878     my $dmn_ip = @$dmn_data[21];
879
880     my $dmn_traffic_limit = @$dmn_data[10];
881
882     my ($dmn_php, $dmn_cgi) = (@$dmn_data[19], @$dmn_data[20]);
883
884     my $conf_dir = $main::cfg{'CONF_DIR'};
885
886     my $tpl_dir = "$conf_dir/apache/parts";
887
888     my $bw_disabled = '';
889
890     if ($dmn_traffic_limit eq 0) {
891         $bw_disabled = '#';
892     }
893
894     my (
895         $dg_b,
896         $dg_entry,
897         $dg_e,
898         $dmn_b,
899         $dmn_entry,
900         $dmn_e,
901         $dmn_cgi_b,
902         $dmn_cgi_entry,
903         $dmn_cgi_e,
904         $dmn_php_b,
905         $dmn_php_entry,
906         $dmn_php_e,
907         $dmn_php2_b,
908         $dmn_php2_entry,
909         $dmn_php2_e,
910             $dmn_awstats_b,
911             $dmn_awstats_dynamic_entry,
912             $dmn_awstats_static_entry,
913             $dmn_awstats_e,
914             $dmn_custom
915        ) = ('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
916
917     (
918      $rs,
919      $dg_b,
920      $dg_entry,
921      $dg_e,
922      $dmn_b,
923      $dmn_entry,
924      $dmn_e,
925      $dmn_cgi_b,
926      $dmn_cgi_entry,
927      $dmn_cgi_e,
928      $dmn_php_b,
929      $dmn_php_entry,
930      $dmn_php_e,
931      $dmn_php2_b,
932      $dmn_php2_entry,
933      $dmn_php2_e,
934          $dmn_awstats_b,
935          $dmn_awstats_dynamic_entry,
936          $dmn_awstats_static_entry,
937          $dmn_awstats_e,
938          $dmn_custom
939     ) = get_tpl (
940                  $tpl_dir,
941                  'dg_b.tpl',
942                  'dg_entry.tpl',
943                  'dg_e.tpl',
944                  'dmn_b.tpl',
945                  'dmn_entry.tpl',
946                  'dmn_e.tpl',
947                  'dmn_cgi_b.tpl',
948                  'dmn_cgi_entry.tpl',
949                  'dmn_cgi_e.tpl',
950                  'dmn_php_b.tpl',
951                  'dmn_php_entry.tpl',
952                  'dmn_php_e.tpl',
953                  'dmn_php2_b.tpl',
954                  'dmn_php2_entry.tpl',
955                  'dmn_php2_e.tpl',
956                  'dmn_awstats_b.tpl',
957                      'dmn_awstats_dynamic_entry.tpl',
958                      'dmn_awstats_static_entry.tpl',
959                      'dmn_awstats_e.tpl',
960                      'custom.conf.tpl'
961                 );
962
963     return ($rs, '') if ($rs != 0);
964
965     my ($suexec_uid, $suexec_gid) = get_dmn_suexec_user($dmn_id);
966
967     my $suexec_user_pref = $main::cfg{'APACHE_SUEXEC_USER_PREF'};
968
969     my ($suexec_user, $suexec_group) = ("$suexec_user_pref$suexec_uid", "$suexec_user_pref$suexec_gid");
970
971     my %tag_hash =
972         (
973          '{DMN_GRP}' => $dmn_grp,
974          '{DMN_NAME}' => $dmn_name,
975          '{SELF}' => $dmn_name,
976          '{DMN_IP}' => $dmn_ip,
977          '{BASE_SERVER_VHOST}' => $main::cfg{'BASE_SERVER_VHOST'},
978          '{WWW_DIR}' => $main::cfg{'APACHE_WWW_DIR'},
979          '{STARTER_DIR}' => $main::cfg{'PHP_STARTER_DIR'},
980          '{PHP_VERSION}' => $main::cfg{'PHP_VERSION'},
981          '{APACHE_LOG_DIR}' => $main::cfg{'APACHE_LOG_DIR'},
982          '{GUI_ROOT_DIR}' => $main::cfg{'GUI_ROOT_DIR'},
983          '{SCOREBOARDS_DIR}' => $main::cfg{'SCOREBOARDS_DIR'},
984          '{PEAR_DIR}' => $main::cfg{'PEAR_DIR'},
985          '{APACHE_USERS_LOG_DIR}' => $main::cfg{'APACHE_USERS_LOG_DIR'},
986          '{CUSTOM_SITES_CONFIG_DIR}' => $main::cfg{'APACHE_CUSTOM_SITES_CONFIG_DIR'},
987          '{SUEXEC_USER}' => $suexec_user,
988          '{SUEXEC_GROUP}' => $suexec_group,
989          '{BWLIMIT}' => $dmn_traffic_limit,
990          '{BWLIMIT_DISABLED}' => $bw_disabled,
991          '##TEMPLATE' => '' #Used to prevent apache from parsing commands which are part of the template
992         );
993
994     (
995      $rs,
996      $dg_b,
997      $dg_entry,
998      $dg_e,
999      $dmn_b,
1000      $dmn_entry,
1001      $dmn_e,
1002      $dmn_cgi_b,
1003      $dmn_cgi_entry,
1004      $dmn_cgi_e,
1005      $dmn_php_b,
1006      $dmn_php_entry,
1007      $dmn_php_e,
1008      $dmn_php2_entry,
1009          $dmn_awstats_dynamic_entry,
1010          $dmn_awstats_static_entry,
1011          $dmn_custom
1012     ) = prep_tpl(
1013                  \%tag_hash,
1014                  $dg_b,
1015                  $dg_entry,
1016                  $dg_e,
1017                  $dmn_b,
1018                  $dmn_entry,
1019                  $dmn_e,
1020                  $dmn_cgi_b,
1021                  $dmn_cgi_entry,
1022                  $dmn_cgi_e,
1023                  $dmn_php_b,
1024                  $dmn_php_entry,
1025                  $dmn_php_e,
1026                  $dmn_php2_entry,
1027                  $dmn_awstats_dynamic_entry,
1028                  $dmn_awstats_static_entry,
1029                  $dmn_custom
1030                 );
1031
1032     return ($rs, '') if ($rs != 0);
1033
1034         #
1035         # Add AWStats data
1036         #
1037
1038         my $awstats_entry = undef;
1039
1040         # AWStats Dynamic
1041         if ($main::cfg{'AWSTATS_ACTIVE'} eq 'yes' && $main::cfg{'AWSTATS_MODE'} eq 0) {
1042                 $awstats_entry = "$dmn_awstats_b\n$dmn_awstats_dynamic_entry\n$dmn_awstats_e";
1043         }
1044         # AWStats Static
1045         elsif ($main::cfg{'AWSTATS_ACTIVE'} eq 'yes' && $main::cfg{'AWSTATS_MODE'} eq 1) {
1046                 $awstats_entry = "$dmn_awstats_b\n$dmn_awstats_static_entry\n$dmn_awstats_e";
1047         }
1048         # No AWStats
1049         else {
1050                 $awstats_entry = "$dmn_awstats_b\n$dmn_awstats_e";
1051         }
1052
1053     ($rs, $dmn_entry) = repl_tag(
1054                                  $dmn_awstats_b,
1055                                  $dmn_awstats_e,
1056                                  $dmn_entry,
1057                                  $awstats_entry
1058                                 );
1059
1060     return ($rs, '') if ($rs != 0);
1061
1062     my $cgi_entry = undef;
1063
1064     if ($dmn_cgi eq 'yes') {
1065
1066         $cgi_entry = "$dmn_cgi_b$dmn_cgi_entry$dmn_cgi_e";
1067
1068     } else {
1069
1070         $cgi_entry = "$dmn_cgi_b$dmn_cgi_e";
1071
1072     }
1073
1074     ($rs, $dmn_entry) = repl_tag(
1075                                  $dmn_cgi_b,
1076                                  $dmn_cgi_e,
1077                                  $dmn_entry,
1078                                  $cgi_entry
1079                                 );
1080
1081     return ($rs, '') if ($rs != 0);
1082
1083     my $php_entry = undef;
1084
1085     if ($dmn_php eq 'no') {
1086
1087         $php_entry = "$dmn_php_b$dmn_php_entry$dmn_php_e";
1088
1089
1090     } else {
1091
1092         $php_entry = "$dmn_php_b$dmn_php_e";
1093
1094         my $php2_entry = "$dmn_php2_b$dmn_php2_entry$dmn_php2_e";
1095
1096         ($rs, $dmn_entry) = repl_tag(
1097                                      $dmn_php2_b,
1098                                      $dmn_php2_e,
1099                                      $dmn_entry,
1100                                      $php2_entry
1101                                     );
1102
1103         return ($rs, '') if ($rs != 0);
1104
1105     }
1106
1107     ($rs, $dmn_entry) = repl_tag(
1108                                  $dmn_php_b,
1109                                  $dmn_php_e,
1110                                  $dmn_entry,
1111                                  $php_entry
1112                                 );
1113
1114     return ($rs, '') if ($rs != 0);
1115
1116     push_el(\@main::el, 'gen_httpd_dmn_group_entry()', "dmn_entry:\n$dmn_entry");
1117
1118     my $dmn_group_entry = "$dmn_b$dmn_entry$dmn_e";
1119
1120     ($rs, $dg_entry) = repl_tag(
1121                                  $dmn_b,
1122                                  $dmn_e,
1123                                  $dg_entry,
1124                                  $dmn_group_entry
1125                                );
1126
1127     #
1128     # Create mod_cband scoreboard for domain group
1129     #
1130
1131     my $scoreboard_file = "$main::cfg{'SCOREBOARDS_DIR'}/$dmn_grp";
1132
1133     $rs = sys_command_rs("$main::cfg{'CMD_TOUCH'} $scoreboard_file");
1134
1135     return $rs if ($rs != 0);
1136
1137     $rs = sys_command_rs("$main::cfg{'CMD_CHOWN'} $main::cfg{'APACHE_USER'}:$main::cfg{'APACHE_GROUP'} $scoreboard_file");
1138
1139     return $rs if ($rs != 0);
1140
1141     #
1142     # Custom domain config file
1143     #
1144
1145     if ( ! -e "$main::cfg{'APACHE_CUSTOM_SITES_CONFIG_DIR'}/$dmn_name.conf" ){
1146
1147         $rs = store_file("$main::cfg{'APACHE_CUSTOM_SITES_CONFIG_DIR'}/$dmn_name.conf", $dmn_custom, 'root', 'root', 0644);
1148
1149         return $rs if ($rs != 0);
1150     }
1151
1152     push_el(\@main::el, 'gen_httpd_dmn_group_entry()', "\n$dg_entry");
1153
1154     return (0,  $dg_entry);
1155
1156 }
1157
1158 ################################################################################
1159 ##
1160 ## Add HTTPd CFG Data
1161 ##
1162
1163 sub dmn_add_httpd_cfg_data {
1164
1165     my ($dmn_data) = @_;
1166
1167     my ($rs, $rdata) = (undef, undef);
1168
1169     push_el(\@main::el, 'dmn_add_httpd_cfg_data()', 'Starting...');
1170
1171     if (!defined($dmn_data) || $dmn_data eq '') {
1172
1173         push_el(\@main::el, 'dmn_add_httpd_cfg_data()', 'ERROR: Undefined Input Data...');
1174
1175         return -1;
1176
1177     }
1178
1179     my $dmn_name = @$dmn_data[1];
1180
1181     my $dmn_grp = $dmn_name;
1182
1183     my $dmn_id = @$dmn_data[0];
1184
1185     my $dmn_ip = @$dmn_data[21];
1186
1187
1188     my $conf_dir = $main::cfg{'CONF_DIR'};
1189
1190
1191     my $tpl_dir = "$conf_dir/apache/parts";
1192
1193     my $working_dir = "$conf_dir/apache/working";
1194
1195     my $backup_dir = "$conf_dir/apache/backup";
1196
1197
1198     my $sys_cfg = "$main::cfg{'APACHE_SITES_DIR'}/ispcp.conf";
1199
1200     my $working_cfg = "$working_dir/ispcp.conf";
1201
1202
1203         if (!(-e $working_cfg)) {
1204
1205                 my $ispcp_base_tpl;
1206
1207                 ($rs, $ispcp_base_tpl) = get_file($tpl_dir."/ispcp_base.tpl");
1208
1209         return $rs if ($rs != 0);
1210
1211                 ($rs, $rdata) = store_file(
1212                                                                         $working_cfg,
1213                                                                         $ispcp_base_tpl,
1214                                                                         "root",
1215                                                                         "root",
1216                                                                         0600
1217                                                                   );
1218
1219                 return $rs if ($rs != 0);
1220
1221     }
1222
1223
1224     my $timestamp = time;
1225
1226     my $backup_cfg = "$backup_dir/httpd.conf.$timestamp";
1227
1228     #
1229     # Getting needed templates;
1230     #
1231
1232     my (
1233         $cfg_b,
1234         $cfg_e,
1235         $vh_b,
1236         $vh_entry,
1237         $vh_e,
1238         $dg_b,
1239         $dg_e
1240        ) = ('', '', '', '', '', '', '');
1241
1242     (
1243      $rs,
1244      $cfg_b,
1245      $cfg_e,
1246      $vh_b,
1247      $vh_entry,
1248      $vh_e,
1249      $dg_b,
1250      $dg_e
1251     ) = get_tpl (
1252                  $tpl_dir,
1253                  'cfg_b.tpl',
1254                  'cfg_e.tpl',
1255                  'vh_b.tpl',
1256                  'vh_entry.tpl',
1257                  'vh_e.tpl',
1258                  'dg_b.tpl',
1259                  'dg_e.tpl'
1260                 );
1261
1262     return $rs if ($rs != 0);
1263
1264
1265     #
1266     # Create apache suexec user for this domain.
1267     #
1268
1269     $rs = add_dmn_suexec_user($d