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

Revision 479, 44.6 kB (checked in by malte, 2 years ago)

--

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