root/tags/omega-1.0.0-rc3/engine/ispcp_common_methods.pl

Revision 971, 42.8 kB (checked in by rats, 10 months ago)

update script ready for testing ... please do NOT use it on productive systems without backups; it's UNTESTED and may cause HEAVY DAMAGE!

little fixes

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