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

Revision 511, 81.1 kB (checked in by rats, 2 years ago)

- DOCS:

  • SuSE: updated installation

- GUI:

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