root/tags/omega-1.0.0-rc2/engine/ispcp_common_code.pl

Revision 479, 41.7 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 BEGIN {
34
35     my @needed = (strict,
36                   warnings,
37                   IO::Socket,
38                   DBI,
39                   DBD::mysql,
40                   MIME::Entity,
41                   MIME::Parser,
42                   Crypt::CBC,
43                   Crypt::Blowfish,
44                   Crypt::PasswdMD5,
45                   MIME::Base64,
46                   Term::ReadPassword,
47                   File::Basename,
48                   File::Path);
49
50     my ($mod, $mod_err, $mod_missing) = ('', '_off_', '');
51
52     for $mod (@needed) {
53
54         if (eval "require $mod") {
55
56             $mod -> import();
57
58         } else {
59
60             print STDERR "\nCRITICAL ERROR: Module [$mod] WAS NOT FOUND !\n" ;
61
62             $mod_err = '_on_';
63
64             if ($mod_missing eq '') {
65
66                 $mod_missing .= $mod;
67
68             } else {
69
70                 $mod_missing .= ", $mod";
71
72             }
73         }
74
75     }
76
77     if ($mod_err eq '_on_') {
78
79         print STDERR "\nModules [$mod_missing] WAS NOT FOUND in your system...\n";
80
81         exit 1;
82
83     } else {
84
85         $| = 1;
86
87     }
88 }
89
90
91 #$main::engine_debug = '_on_';
92
93 $main::cc_stdout = '/tmp/ispcp-cc.stdout';
94
95 $main::cc_stderr = '/tmp/ispcp-cc.stderr';
96
97 $main::el_sep = "\t#\t";
98
99 @main::el = ();
100
101
102 sub push_el {
103
104     my ($el, $sub_name, $msg) = @_;
105
106     push @$el, "$sub_name".$main::el_sep."$msg";
107
108     if (defined($main::engine_debug)) {
109
110         print STDOUT "DEBUG: push_el() sub_name: $sub_name, msg: $msg\n";
111
112     }
113
114
115 }
116
117 sub pop_el {
118
119     my ($el) = @_;
120
121     my $data = pop @$el;
122
123     if (!defined($data)) {
124
125         if (defined($main::engine_debug)) {
126
127             print STDOUT "DEBUG: pop_el() Empty 'EL' Stack !\n";
128
129         }
130
131         return undef;
132     }
133
134     my ($sub_name, $msg) = split(/$main::el_sep/, $data);
135
136     if (defined($main::engine_debug)) {
137
138         print STDOUT "DEBUG: pop_el() sub_name: $sub_name, msg: $msg\n";
139
140     }
141
142
143     return $data;
144
145 }
146
147
148 sub dump_el {
149
150     my ($el, $fname) = @_;
151
152     my $res;
153
154     if ($fname ne 'stdout') {
155
156         $res = open(FP, ">", $fname);
157
158         if (!defined($res)) {
159
160             return 0;
161
162         }
163
164     }
165
166     my $el_data = undef;
167
168     #
169     #if ($fname eq 'stdout') {
170     #
171     #    print STDOUT "%-20s | %s\n",  ' function', 'message';
172     #
173     #    print STDOUT "---------------------|---------------------------------------------------------------\n";
174     #
175     #} else {
176     #
177     #    print FP "%-20s | %s\n",  ' function', 'message';
178     #
179     #    print FP "---------------------|---------------------------------------------------------------\n";
180     #
181     #}
182     #
183
184     while (defined($el_data = pop_el(\@main::el))) {
185
186         my ($sub_name, $msg) = split(/$main::el_sep/, $el_data);
187
188         if ($fname eq 'stdout') {
189
190             printf STDOUT "%-30s | %s\n",  $sub_name, $msg;
191
192         } else {
193
194             printf FP "%-30s | %s\n",  $sub_name, $msg;
195
196         }
197
198     }
199
200     close(FP);
201
202 }
203
204 # Global variables;
205
206 $main::db_host = undef;
207
208 $main::db_user = undef;
209
210 $main::db_pwd = undef;
211
212 $main::db_name = undef;
213
214 @main::db_connect = ();
215
216 $main::db = undef;
217
218 #
219
220 sub doSQL {
221
222     my ($sql) = @_;
223
224     my $qr = undef;
225
226     push_el(\@main::el, 'doSQL()', 'Starting...');
227
228     if (!defined($sql) || ($sql eq '')) {
229
230         push_el(\@main::el, 'doSQL()', 'ERROR: Undefined SQL query !');
231
232         return (-1, '');
233
234     }
235
236     if (!defined($main::db) || !ref($main::db)) {
237
238         $main::db = DBI -> connect(@main::db_connect, {PrintError => 0});
239
240         if ( !defined($main::db) ) {
241
242             push_el(
243                     \@main::el,
244                     'doSQL()',
245                     'ERROR: Unable to connect SQL server !'
246                    );
247
248             return (-1, '');
249
250         }
251     }
252
253     if ($sql =~ /select/i) {
254
255         $qr = $main::db -> selectall_arrayref($sql);
256
257     } elsif ($sql =~ /show/i) {
258
259         $qr = $main::db -> selectall_arrayref($sql);
260
261     } else {
262
263         $qr = $main::db -> do($sql);
264
265     }
266
267     if (defined($qr)) {
268
269         push_el(\@main::el, 'doSQL()', 'Ending...');
270
271         return (0, $qr);
272
273     } else {
274
275         push_el(\@main::el, 'doSQL()', 'ERROR: Incorrect SQL Query -> '.$main::db -> errstr);
276
277         return (-1, '');
278
279     }
280
281 }
282
283 sub setfmode {
284
285     my ($fname, $fuid, $fgid, $fperms) = @_;
286
287     push_el(\@main::el, 'setfmode()', 'Starting...');
288
289     if (
290         !defined($fname) || !defined($fuid) ||
291         !defined($fgid) || !defined($fperms) ||
292         $fname eq '' || $fuid eq '' ||
293         $fgid eq '' || $fperms eq ''
294        )
295     {
296
297         push_el(
298                 \@main::el,
299                 'setfmode()',
300                 "ERROR: Undefined input data, fname: |$fname|, fuid: |$fuid|, fgid: |$fgid|, fperms: |$fperms| !"
301                );
302
303         return -1;
304
305     }
306
307     if (! -e $fname) {
308
309         push_el(
310                 \@main::el,
311                 'setfmode()',
312                 "ERROR: File '$fname' does not exist !"
313                );
314
315         return -1;
316     }
317
318     my @udata = ();
319
320     my @gdata = ();
321
322     my ($uid, $gid) = ($fuid, $fgid);
323
324         if ($fuid =~ /^\d+$/) {
325
326                 $uid = $fuid;
327
328     } elsif ($fuid ne '-1') {
329
330         @udata = getpwnam($fuid);
331
332         if (scalar(@udata) == 0) {
333
334             push_el(
335                     \@main::el,
336                     'setfmode()',
337                     "ERROR: Unknown user '$fuid' !"
338                    );
339
340             return -1;
341
342         }
343
344         $uid = $udata[2];
345     }
346
347         if ($fgid =~ /^\d+$/) {
348
349                 $gid = $fgid;
350
351         } elsif ($fgid ne '-1') {
352
353         @gdata = getgrnam($fgid);
354
355         if (scalar(@gdata) == 0) {
356
357             push_el(
358                     \@main::el,
359                     'setfmode()',
360                     "ERROR: Unknown group '$fgid' !"
361                    );
362
363             return -1;
364
365         }
366
367         $gid = $gdata[2];
368     }
369
370     my $res = chmod ($fperms, $fname);
371
372     if ($res != 1) {
373
374         push_el(
375                 \@main::el,
376                 'setfmode()',
377                 "ERROR: Can not change permissions of file '$fname' !"
378                );
379
380         return -1;
381
382     }
383
384     $res = chown ($uid, $gid, $fname);
385
386     if ($res != 1) {
387
388         push_el(
389                 \@main::el,
390                 'setfmode()',
391                 "ERROR: Can not change user/group of file '$fname' !"
392                );
393
394         return -1;
395
396     }
397
398     push_el(\@main::el, 'setfmode()', 'Ending...');
399
400     return 0;
401
402 }
403
404 sub get_file {
405
406     my ($fname) = @_;
407
408     push_el(\@main::el, 'get_file()', 'Starting...');
409
410     if (!defined($fname) || ($fname eq '')) {
411
412         push_el(
413                 \@main::el,
414                 'get_file()',
415                 "ERROR: Undefined input data, fname: |$fname| !"
416                );
417
418         return (-1, '');
419
420     }
421
422     if (! -e $fname) {
423
424         push_el(
425                 \@main::el,
426                 'get_file()',
427                 "ERROR: File '$fname' does not exist !"
428                );
429
430         return (-1, '');
431
432     }
433
434     my $res = open(F, '<', $fname);
435
436     if (!defined($res)) {
437
438         push_el(
439                 \@main::el,
440                 'get_file()',
441                 "ERROR: Can't open '$fname' for reading !"
442                );
443
444         return (-1, '');
445
446     }
447
448     my @fdata = <F>;
449
450     close(F);
451
452     my $line = join('', @fdata);
453
454     push_el(\@main::el, 'get_file()', 'Ending...');
455
456     return (0, $line);
457
458 }
459
460 sub store_file {
461
462     my ($fname, $fdata, $fuid, $fgid, $fperms) = @_;
463
464     push_el(\@main::el, 'store_file()', 'Starting...');
465
466     if (
467         !defined($fname) || !defined($fuid) ||
468         !defined($fgid) || !defined($fperms) ||
469         $fname eq '' || $fuid eq '' ||
470         $fgid eq '' || $fperms eq ''
471        )
472     {
473         push_el(
474                 \@main::el,
475                 'store_file()',
476                 "ERROR: Undefined input data, fname: |$fname|, fdata, fuid: '$fuid', fgid: '$fgid', fperms: '$fperms'"
477                );
478
479         return -1;
480     }
481
482     my $res = open(F, '>', $fname);
483
484     if (!defined($res)) {
485
486         push_el(
487                 \@main::el,
488                 'store_file()',
489                 "ERROR: Can't open file |$fname| for writing !"
490                );
491
492         return -1;
493
494     }
495
496     print F $fdata;
497
498     close(F);
499
500     my ($rs, $rdata) = setfmode($fname, $fuid, $fgid, $fperms);
501
502     return -1 if ($rs != 0);
503
504     push_el(\@main::el, 'store_file()', 'Ending...');
505
506     return 0;
507
508 }
509
510 sub del_file {
511
512     my ($fname) = @_;
513
514     push_el(\@main::el, 'del_file()', 'Starting...');
515
516     if (!defined($fname) || ($fname eq '')) {
517
518         push_el(
519                 \@main::el,
520                 'del_file()',
521                 "ERROR: Undefined input data, fname: |$fname| !"
522                );
523
524         return -1;
525
526     }
527
528     if (! -e $fname) {
529
530         push_el(
531                 \@main::el,
532                 'del_file()',
533                 "ERROR: File '$fname' does not exist !"
534                );
535
536         return -1;
537
538     }
539
540     my $res = unlink ($fname);
541
542     if ($res != 1) {
543
544         push_el(
545                 \@main::el,
546                 'del_file()',
547                 "ERROR: Can't unlink '$fname' !"
548                );
549
550         return -1;
551
552     }
553
554     push_el(\@main::el, 'del_file()', 'Ending...');
555
556     return 0;
557
558 }
559
560 sub set_zone {
561     my ($fdata, $data, $zone, $comment) = @_;
562
563     my @fdata = split("\n", $fdata);
564
565     my $bz = '';
566     my $az = '';
567     my $zs = 0;
568     my $ze = 0;
569     my $ll;
570     my $curline;
571
572     while(length($fdata) > 0) {
573         $ll = index($fdata, "\n");
574         if( $ll < 0 ) {
575             $ll = length( $fdata );
576         } else {
577             $ll++;
578         }
579         $curline = substr( $fdata, 0, $ll );
580         $fdata = substr( $fdata, $ll );
581
582         if( $zs == 0 ) {
583             if( index($curline, $comment."## START ISPCP ".$zone." ###") == 0 ) {
584                 $zs = 1;
585             } else {
586                 $bz .= $curline;
587             }
588         } elsif( $ze == 0 ) {
589             if( index($curline, $comment."## END ISPCP ".$zone." ###") == 0) {
590                 $ze = 1;
591             }
592         } elsif( $ze == 1 ) {
593                 $az .= $curline;
594         }
595     }
596
597     return
598         $bz.($zs == 1 ? "" : "\n").
599         $comment."## START ISPCP ".$zone." ###\n".
600         $data."\n".
601         $comment."## END ISPCP ".$zone." ###\n".
602         $az;
603 }
604
605 sub get_zone {
606     my ($fdata, $zone, $comment) = @_;
607
608     my @fdata = split("\n", $fdata);
609
610     my $zonecontent = '';
611     my $zs = 0;
612     my $ze = 0;
613     my $ll;
614     my $curline;
615
616     while(length($fdata) > 0) {
617         $ll = index($fdata, "\n");
618         if( $ll < 0 ) {
619             $ll = length( $fdata );
620         } else {
621             $ll++;
622         }
623         $curline = substr( $fdata, 0, $ll );
624         $fdata = substr( $fdata, $ll );
625
626         if( $zs == 0 ) {
627             if( index($curline, $comment."## START ISPCP ".$zone." ###") == 0 ) {
628                 $zs = 1;
629             }
630         } elsif( $ze == 0 ) {
631             if( index($curline, $comment."## END ISPCP ".$zone." ###") == 0) {
632                 $ze = 1;
633             } else {
634                 $zonecontent .= $curline;
635             }
636         }
637     }
638
639     return $zonecontent;
640 }
641
642 sub del_zone {
643     my ($fdata, $zone, $comment) = @_;
644
645     my @fdata = split("\n", $fdata);
646
647     my $bz = '';
648     my $az = '';
649     my $zs = 0;
650     my $ze = 0;
651     my $ll;
652     my $curline;
653
654     while(length($fdata) > 0) {
655         $ll = index($fdata, "\n");
656         if( $ll < 0 ) {
657             $ll = length( $fdata );
658         } else {
659             $ll++;
660         }
661         $curline = substr( $fdata, 0, $ll );
662         $fdata = substr( $fdata, $ll );
663
664         if( $zs == 0 ) {
665             if( index($curline, $comment."## START ISPCP ".$zone." ###") == 0 ) {
666                 $zs = 1;
667             } else {
668                 $bz .= $curline;
669             }
670         } elsif( $ze == 0 ) {
671             if( index($curline, $comment."## END ISPCP ".$zone." ###") == 0) {
672                 $ze = 1;
673             }
674         } elsif( $ze == 1 ) {
675                 $az .= $curline;
676         }
677     }
678
679     return $bz.$az;
680 }
681
682 sub sys_command {
683
684     my ($cmd) = @_;
685
686     push_el(\@main::el, 'sys_command()', 'Starting...');
687
688     my $result = system($cmd);
689
690     my $exit_value  = $? >> 8;
691
692     my $signal_num  = $? & 127;
693
694     my $dumped_core = $? & 128;
695
696     if ($exit_value == 0) {
697
698         push_el(\@main::el, "sys_command('$cmd')", 'Ending...');
699
700         return 0;
701
702     } else {
703
704         push_el(\@main::el, 'sys_command()', "ERROR: External command '$cmd' returned '$exit_value' status !");
705
706         return -1;
707
708     }
709
710 }
711
712 sub sys_command_rs {
713
714     my ($cmd) = @_;
715
716     push_el(\@main::el, 'sys_command_rs()', 'Starting...');
717
718     my $result = system($cmd);
719
720     my $exit_value  = $? >> 8;
721
722     my $signal_num  = $? & 127;
723
724     my $dumped_core = $? & 128;
725
726     push_el(\@main::el, 'sys_command_rs()', 'Ending...');
727
728     if ($exit_value == 0) {
729
730         return 0;
731
732     } else {
733
734
735         return $exit_value;
736
737     }
738
739 }
740
741 sub make_dir {
742
743     my ($dname, $duid, $dgid, $dperms) = @_;
744
745     my ($rs, $rdata) = ('', '');
746
747     push_el(\@main::el, 'make_dir()', 'Starting...');
748
749     if (
750         !defined($dname) || !defined($duid) ||
751         !defined($dgid) || !defined($dperms) ||
752         $dname eq '' || $duid eq '' ||
753         $dgid eq '' || $dperms eq ''
754        )
755     {
756
757         push_el(\@main::el, 'make_dir()', "ERROR: Undefined input data, dname: |$dname|, duid: |$duid|, dgid: |$dgid|, dperms: |$dperms| !");
758
759         return -1;
760
761     }
762
763     if ( -e $dname && -f $dname ) {
764
765         push_el(\@main::el,'make_dir()', "'$dname' exists as file ! removing file first...");
766
767         return -1 if (del_file($dname) != 0);
768
769     }
770
771     if (!(-e $dname && -d $dname)) {
772
773         push_el(\@main::el, 'make_dir()', "'$dname' doesn't exists as directory! creating...");
774
775         $rs =  mkpath($dname);
776
777         if (!$rs) {
778
779             push_el(\@main::el, 'make_dir()', "ERROR: mkdir() returned '$rs' status !");
780
781             return -1;
782
783         }
784
785     } else {
786
787         push_el(\@main::el, 'make_dir()', "'$dname' exists ! Setting its permissions...");
788
789     }
790
791     return -1 if (setfmode($dname, $duid, $dgid, $dperms) != 0);
792
793     push_el(\@main::el, 'make_dir()', 'Ending...');
794
795     return 0;
796 }
797
798 sub del_dir {
799
800     my ($dname) = @_;
801
802     push_el(\@main::el, 'make_dir()', 'Starting...');
803
804     if (!defined($dname) || ($dname eq '')) {
805
806         push_el(\@main::el, 'make_dir()', "ERROR: Undefined input data, dname: |$dname| !");
807
808         return -1;
809
810     }
811
812     push_el(\@main::el, 'make_dir()', "Trying to remove '$dname'...");
813
814     return -1 if (sys_command("rm -rf $dname") != 0);
815
816     push_el(\@main::el, 'make_dir()', 'Ending...');
817
818     return 0;
819
820 }
821
822 sub gen_rand_num {
823
824     my ($len) = @_;
825
826     push_el(\@main::el, 'gen_rand_num()', 'Starting...');
827
828     if (!defined($len) || ($len eq '')) {
829
830         push_el(\@main::el, 'gen_rand_num()', "ERROR: Undefined input data, len: |$len| !");
831
832         return (-1, '');
833
834     }
835
836     if (!(0 < $len && $len < 11)) {
837
838         push_el(\@main::el, 'gen_rand_num()', "ERROR: Input data length '$len' out of limits [1, 10] !");
839
840         return (-1, '');
841
842     }
843
844     my @rand_data = ('A'..'Z', 'a'..'z', '0'..'9', '.', '/');
845
846     my ($i, $rdata) = ('', '');
847
848     for ($i = 0; $i < $len; $i++) {
849
850         $rdata .= $rand_data[ rand() * ($#rand_data + 1) ];
851
852     }
853
854     push_el(\@main::el, 'gen_rand_num()', 'Ending...');
855
856     return (0, $rdata);
857
858 }
859
860 sub crypt_md5_data {
861
862     my ($data) = @_;
863
864     push_el(\@main::el, 'crypt_md5_data()', 'Starting...');
865
866     if (!defined($data) || $data eq '') {
867
868         push_el(\@main::el, 'crypt_md5_data()', "ERROR: Undefined input data, data: |$data| !");
869
870         return (-1, '');
871
872     }
873
874     my ($rs, $rdata) = gen_rand_num(2);
875
876     return (-1, '') if ($rs != 0);
877
878     $rdata = unix_md5_crypt($data, $rdata);
879
880     push_el(\@main::el, 'crypt_md5_data()', 'Ending...');
881
882     return (0, $rdata);
883
884 }
885
886 sub crypt_data {
887
888     my ($data) = @_;
889
890     push_el(\@main::el, 'crypt_data()', 'Starting...');
891
892     if (!defined($data) || $data eq '') {
893
894         push_el(\@main::el, 'crypt_data()', "ERROR: Undefined input data, data: |$data| !");
895
896         return (-1, '');
897
898     }
899
900     my ($rs, $rdata) = gen_rand_num(2);
901
902     return (-1, '') if ($rs != 0);
903
904     $rdata = crypt($data, $rdata);
905
906     push_el(\@main::el, 'crypt_data()', 'Ending...');
907
908     return (0, $rdata);
909
910 }
911
912 sub get_tag {
913
914     my ($bt, $et, $src) = @_;
915
916     push_el(\@main::el, 'get_tag()', "Starting...");
917
918     if (
919         !defined($bt) || !defined($et) ||
920         !defined($src) || $bt eq '' ||
921         $et eq '' || $src eq ''
922        )
923     {
924
925         push_el(\@main::el, 'get_tag()', "ERROR: Undefined intput data, bt: |$bt|, et: |$et|, src !");
926
927         return (-1, '');
928
929     }
930
931     my ($bt_len, $et_len, $src_len) = (
932                                        length($bt),
933                                        length($et),
934                                        length($src)
935                                       );
936
937     #
938     #return ('_e03_', $main::strerr{'_e03_'})
939     #
940     #if ($bt_len > $src_len || $et_len > $src_len);
941     #
942
943     if ($bt eq $et) {
944
945
946         # Let's search for ...$tag... ;
947
948         # $bt == $et == $tag ;
949
950
951         my $tag = $bt;
952
953         my $tag_pos = index($src, $tag);
954
955         if ($tag_pos < 0) {
956
957             push_el(\@main::el, 'get_tag()', "ERROR: '$bt' eq '$et', missing '$bt' in src !");
958
959             return (-4, '');
960
961         } else {
962
963             push_el(\@main::el, 'get_tag()', 'Ending...');
964
965             return (0, $tag);
966
967         }
968
969     } else {
970
971         if ($bt_len + $et_len > $src_len) {
972
973             push_el(\@main::el, 'get_tag()', "ERROR: len($bt) + len($et) > len(src) !");
974
975             return (-1, '');
976
977         }
978
979
980         # Let's search for ...$bt...$et... ;
981
982
983         my ($bt_pos, $et_pos) = (index($src, $bt), index($src, $et));
984
985         if ($bt_pos < 0 || $et_pos < 0) {
986
987             push_el(\@main::el, 'get_tag()', "ERROR: '$bt' ne '$et', '$bt' or '$et' missing in src !");
988
989             return (-5, '');
990
991         }
992
993         if ($et_pos < $bt_pos + $bt_len) {
994
995             push_el(\@main::el, 'get_tag()', "ERROR: '$bt' ne '$et', '$et' overlaps '$bt' in src !");
996
997             return (-1, '');
998
999         }
1000
1001         push_el(\@main::el, 'get_tag()', 'Ending...');
1002
1003         my $tag_len = $et_pos + $et_len - $bt_pos;
1004
1005         return (0, substr($src, $bt_pos, $tag_len));
1006
1007     }
1008
1009 }
1010
1011 sub repl_tag {
1012
1013     my ($bt, $et, $src, $rwith) = @_;
1014
1015     push_el(\@main::el, 'repl_tag()', "Starting...");
1016
1017     if (!defined ($rwith)) {
1018
1019         push_el(\@main::el, 'repl_tag()', "ERROR: Undefined input data, rwith: |$rwith| !");
1020
1021         return (-1, '');
1022
1023     }
1024
1025     my ($rs, $rdata) = get_tag($bt, $et, $src);
1026
1027     return $rs if ($rs != 0);
1028
1029     my $tag = $rdata;
1030
1031     my ($tag_pos, $tag_len) = (index($src, $tag), length($tag));
1032
1033     if ($rwith eq '') {
1034
1035         substr($src, $tag_pos, $tag_len, '');
1036
1037     } else {
1038
1039         substr($src, $tag_pos, $tag_len, $rwith);
1040
1041     }
1042
1043     push_el(\@main::el, 'repl_tag()', "Ending...");
1044
1045     return (0, $src);
1046 }
1047
1048 sub add_tag {
1049
1050     my ($bt, $et, $src, $adata) = @_;
1051
1052     push_el(\@main::el, 'add_tag()', "Starting...");
1053
1054     if (!defined($adata) || $adata eq '') {
1055
1056         push_el(\@main::el, 'add_tag()', "ERROR: Undefined input data, adata: |$adata| !");
1057
1058         return (-1, '');
1059     }
1060
1061     my ($rs, $rdata) = get_tag($bt, $et, $src);
1062
1063     return ($rs, '') if ($rs != 0);
1064
1065     my $rwith = '';
1066
1067     if ($bt eq $et) {
1068
1069         $rwith = "$adata$bt";
1070
1071     } else {
1072
1073         $rwith = "$adata$bt$et";
1074
1075     }
1076
1077     ($rs, $rdata) = repl_tag($bt, $et, $src, $rwith);
1078
1079     return (-1, '') if ($rs != 0);
1080
1081     push_el(\@main::el, 'add_tag()', "Ending...");
1082
1083     return (0, $rdata);
1084 }
1085
1086 sub del_tag {
1087
1088     my ($bt, $et, $src) = @_;
1089
1090     push_el(\@main::el, 'del_tag()', "Starting...");
1091
1092     my ($rs, $rdata) = get_tag($bt, $et, $src);
1093
1094     return ($rs, '') if ($rs != 0);
1095
1096     ($rs, $rdata) = repl_tag($bt, $et, $src, '');
1097
1098     return (-1, '') if ($rs != 0);
1099
1100     push_el(\@main::el, 'del_tag()', "Ending...");
1101
1102     return (0, $rdata);
1103
1104 }
1105
1106 sub get_var {
1107
1108     my ($var, $src) = @_;
1109
1110     push_el(\@main::el, 'get_var()', "Starting...");
1111
1112     my ($rs, $rdata) = get_tag($var, $var, $src);
1113
1114     return ($rs, '') if ($rs != 0);
1115
1116     push_el(\@main::el, 'get_var()', "Ending...");
1117
1118     return (0, $rdata);
1119
1120 }
1121
1122 sub repl_var {
1123
1124     my ($var, $src, $rwith) = @_;
1125
1126     my ($rs, $rdata, $result) = (0, $src, '');
1127
1128     push_el(\@main::el, 'repl_var()', "Starting...");
1129
1130     while ($rs == 0) {
1131
1132         $result = $rdata;
1133
1134         ($rs, $rdata) = repl_tag($var, $var, $rdata, $rwith);
1135
1136         return -1 if ($rs != 0 && $rs != -4);
1137
1138     }
1139
1140     push_el(\@main::el, 'repl_var()', "Ending...");
1141
1142     return (0, $result);
1143 }
1144
1145 sub add_var {
1146
1147     my ($var, $src, $adata) = @_;
1148
1149     push_el(\@main::el, 'add_var()', "Starting...");
1150
1151     my ($rs, $rdata) = add_tag($var, $var, $src, $adata);
1152
1153     return -1 if ($rs != 0);
1154
1155     push_el(\@main::el, 'add_var()', "Ending...");
1156
1157     return (0, $rdata);
1158
1159 }
1160
1161 sub del_var {
1162
1163     my ($var, $src) = @_;
1164
1165     push_el(\@main::el, 'del_var()', "Starting...");
1166
1167     my ($rs, $rdata) = repl_var($var, $src, '');
1168
1169     return -1 if ($rs != 0);
1170
1171     push_el(\@main::el, 'del_var()', "Ending...");
1172
1173     return ($rs, $rdata);
1174
1175 }
1176
1177 sub get_tpl {
1178
1179     my $tpl_dir = $_[0];
1180
1181     my @tpls = @_;
1182
1183     my ($rs, $rdata, $tpl_file) = ('', '', '');
1184
1185     my @res = (0);
1186
1187     push_el(\@main::el, 'get_tpl()', "Starting...");
1188
1189     if (scalar(@tpls) < 2) {
1190
1191         push_el(\@main::el, 'get_tpl()', "ERROR: Template filename(s) missing !");
1192
1193         return (-1, '');
1194
1195     }
1196
1197     shift(@tpls);
1198
1199     foreach (@tpls) {
1200
1201         $tpl_file = $_;
1202
1203         ($rs, $rdata) = get_file("$tpl_dir/$tpl_file");
1204
1205         return (-1, '') if ($rs != 0);
1206
1207         push (@res, $rdata);
1208     }
1209
1210     push_el(\@main::el, 'get_tpl()', "Ending...");
1211
1212     return @res;
1213
1214 }
1215
1216 sub prep_tpl {
1217
1218     my $hash_ptr = $_[0];
1219
1220     my @tpls = @_;
1221
1222     my ($rs, $rdata) = ('', '', '');
1223
1224     my @res = (0);
1225
1226     push_el(\@main::el, 'prep_tpl()', "Starting...");
1227
1228     if (scalar(@tpls) < 2) {
1229
1230         push_el(\@main::el, 'prep_tpl()', "ERROR: Template variable(s) missing !");
1231
1232         return (-1, '');
1233
1234     }
1235
1236     shift(@tpls);
1237
1238     my ($i, $key) = ('', '');
1239
1240     for($i = 0; $i < scalar(@tpls); $i++) {
1241
1242         foreach $key (keys %$hash_ptr) {
1243
1244             my $name = $key;
1245
1246             my $value = $hash_ptr -> {$key};
1247
1248             ($rs, $rdata) = repl_var($name, $tpls[$i], $value);
1249
1250             return (-1, '') if ($rs != 0);
1251
1252             $tpls[$i] = $rdata;
1253
1254         }
1255
1256         push (@res, $tpls[$i]);
1257     }
1258
1259     push_el(\@main::el, 'prep_tpl()', "Ending...");
1260
1261     return @res;
1262 }
1263
1264 $main::lock_file = '/tmp/ispcp.lock';
1265
1266 sub lock_system {
1267
1268     push_el(\@main::el, 'lock_system()', 'Starting...');
1269
1270     if (-e $main::lock_file) {
1271
1272         push_el(\@main::el, 'lock_system()', 'ERROR: request engine already locked !');
1273
1274         return -1;
1275
1276     }
1277
1278     my $touch_cmd = "`which touch` $main::lock_file";
1279
1280     my $rs = sys_command($touch_cmd);
1281
1282     return -1 if ($rs != 0);
1283
1284     push_el(\@main::el, 'lock_system()', 'Ending...');
1285
1286     return 0;
1287 }
1288
1289 sub unlock_system {
1290
1291     push_el(\@main::el, 'unlock_system()', 'Starting...');
1292
1293     my $rm_cmd = "`which rm` -rf $main::lock_file";
1294
1295     my $rs = sys_command($rm_cmd);
1296
1297     return -1 if ($rs != 0);
1298
1299     push_el(\@main::el, 'unlock_system()', 'Ending...');
1300
1301     return 0;
1302
1303 }
1304
1305 # License request function must not SIGPIPE;
1306
1307 $SIG{PIPE} = 'IGNORE';
1308
1309 $SIG{HUP} = 'IGNORE';
1310
1311 sub connect_ispcp_daemon {
1312
1313     push_el(\@main::el, 'connect_ispcp_daemon()', 'Starting...');
1314
1315     my $fd = IO::Socket::INET -> new(
1316                                      Proto => "tcp",
1317                                      PeerAddr => "127.0.0.1",
1318                                      PeerPort => "8668"
1319                                     );
1320
1321     if (!defined($fd)) {
1322
1323         push_el(\@main::el, 'connect_ispcp_daemon()', "ERROR: Can't connect to ISPCP license daemon !");
1324
1325         return (-1, '');
1326
1327     }
1328
1329     push_el(\@main::el, 'connect_ispcp_daemon()', 'Ending...');
1330
1331     return (0, $fd);
1332 }
1333
1334 sub recv_line {
1335
1336     my ($fd) = @_;
1337
1338     my ($res, $row, $ch) = (undef, undef, undef, undef);
1339
1340     push_el(\@main::el, 'recv_line()', 'Starting...');
1341
1342     do {
1343
1344         $res = recv($fd, $ch, 1, 0);
1345
1346         if (!defined($res)) {
1347
1348             push_el(\@main::el, 'recv_line()', "ERROR: unexpected IO prebolems !");
1349
1350             return (-1, '');
1351
1352         }
1353
1354         $row .= $ch;
1355
1356     } while ($ch ne "\n");