-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxms_install.pl
More file actions
executable file
·4752 lines (4017 loc) · 152 KB
/
Copy pathxms_install.pl
File metadata and controls
executable file
·4752 lines (4017 loc) · 152 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
######################################################################
#
# Dialogic(r) PowerMedia eXtended Media Server installation
#
# Copyright (C) 2001-2013 Dialogic Corporation. All Rights Reserved.
#
# All names, products, and services mentioned herein are the
# trademarks or registered trademarks of their respective
# organizations and are the sole property of their respective
# owners.
#
######################################################################
use 5.10.1;
use strict;
use feature 'state';
use Getopt::Long;
use File::Copy;
use File::Temp;
use File::Basename;
use File::Path qw(remove_tree make_path);
use Class::Struct;
use DirHandle;
use Cwd qw(abs_path cwd);
#*********************************************
# Globals
#*********************************************
my $Xms_File_Version = '';
my $Xms_Installed_Version = '';
my $Xms_HMP_Installed_Version = '';
use constant { XIS_NOT_INSTALLED => 0,
XIS_PARTIAL => 1,
XIS_INSTALLED => 2
};
my $Xms_Install_Status = XIS_NOT_INSTALLED;
my $Xms_Pkg_Dir = './xms_distribution';
# Operating modes
use constant { IM_NONE => 0,
IM_INSTALL => 1,
IM_UPDATE => 2,
IM_REMOVE => 4,
IM_TEST => 8,
};
my $Xms_Install_Mode = IM_INSTALL;
use constant { OSCA_CHECK => 0,
OSCA_CONFIGURE => 1,
};
# Linux Distributions and minimum versions supported by this XMS release
my %Distros_Supported = (
# distro name ( in /etc/system-release) => minimum version
'Red Hat' => '6.2',
'CentOS' => '6.2',
'Oracle' => '6.2',
#SuSE => '11.2',
);
my $Distro_ID = "";
####################################################
# Package name lists
# format must be just the package name (from rpm -qi -p pkg.rpm) or
# name.arch if the required arch is not x86_64
###################################################
# Required Packages from the standard Centos and RHEL distribution
my @Pkgs_Distro_Required = qw(
bzip2 bzip2-libs.i686 cairo cairo.i686 expect fontconfig.i686 freetype.i686 ghostscript.i686 glib2 gtk2 gtk2.i686
httpd mod_perl perl-BSD-Resource jasper-libs.i686 lcms-libs.i686 libgomp.i686 libICE.i686 libpng.i686 librsvg2.i686
libstdc++ libtool libuuid.i686 libxml2 libX11.i686 libSM.i686 libtiff.i686 libtool-ltdl.i686
libwmf-lite.i686 libXt.i686 mod_ssl ncurses ncurses-libs.i686 ntp openssl openssl.i686 pango pcre pcre.i686
php-cli php-common php-ldap php php-pdo redhat-lsb sox telnet uuid zlib.i686 tcpdump
libidn.i686 cyrus-sasl-lib.i686 openldap.i686 nss nss.i686 nss-util.i686 nss-softokn.i686 db4.i686 readline.i686 sqlite.i686 libssh2.i686
libcurl.i686 nspr.i686 nfs-utils nfs-utils-lib libtirpc libgssglue libevent rpcbind
libraw1394 libdc1394 SDL speex libcdio pulseaudio-libs net-snmp net-snmp-libs net-snmp-libs.i686
);
# Prerequisite packages requiring special processing
my @Pkgs_Distro_Special = qw( nss-sysinit.i686 libjpeg-turbo.i686 libjpeg-turbo libjpeg.i686 libjpeg);
# Incompatible (conflicting) OS packages that must be removed before installling XMS.
my @Pkgs_Distro_Incompatible = qw(ImageMagick.x86_64 );
# Supplied third party packages that are not available in all supported distributions
my @Pkgs_Support = qw(
zeromq.i386 xerces-c.i686 fcgi.i686 dojo.noarch lighttpd lighttpd-fastcgi spawn-fcgi js.i686
libwebsockets.i386 ImageMagick.i686 ImageMagick-c++.i686 ilbc x264 x264-libs ffmpeg-libs ffmpeg opus
lame-libs xvidcore svgalib libass schroedinger orc libv4l librtmp openal-soft libva1 enca celt
);
# Core XMS components
my @Pkgs_Xms_Components = qw(appmanager.i386 broker.i386 libxms.i386 restful.i386 xmserver.i386 xmsadmin.noarch nodecontroller.i386
vxml.i386 httpclient.i386 netann.i386 libmrcp.i386 mrcpclient.i386 hlsserver.i386 msml.i386 rtcweb.i386 phrserver.noarch snmpsubagent.i386
perfmanager-libs.i386 perfmanager.i386 );
# Hmp media packages
my @Pkgs_Hmp_Media = qw(lsb-dialogic-hmp41-msp.noarch );
my @Pkgs_Hmp_Sybsystem = qw(lsb-dialogic-hmp41-com.i386 lsb-dialogic-hmp41-dmdev.i386 lsb-dialogic-hmp41-docs.noarch lsb-dialogic-hmp41-hmp.i386
lsb-dialogic-hmp41-lic.i386 lsb-dialogic-hmp41-x64com lsb-dialogic-hmp41-x64hmp);
#
# package management
#
my %Pkg_Groups;
struct Pkg => { name => '$',
filename => '$',
path => '$',
file_version => '$',
file_release => '$',
inst_version => '$',
inst_release => '$',
arch => '$',
is_installed => '$',
file_size => '$',
inst_size => '$'
};
struct PkgGrp => { name => '$',
dir => '$',
no_post_upg => '$',
no_check_ver => '$',
file_required => '$',
group => '@'
};
struct Srv => { name => '$',
description => '$'
};
struct Hmp => { path => '$',
dist_version => '$',
inst_version => '$',
cli_telnet_port => '$',
rtp_address => '$',
_tar_dir => '$',
_media_pkgs => 'PkgGrp',
_hmp_pkgs => 'PkgGrp'
};
#
# Global Errno style return codes
#
use constant {
E_OK => 0,
E_GEN_FAILED => 1, # general error
E_GEN_COMMAND_LINE => 2, # command line parameter error
E_GEN_DIST_CORRUPTED => 3, # The distribution is corrupted (missing file(s) etc.)
E_GEN_XMS_CORRUPTED => 4, # The currently installed XMS is corrupted (not fully installed , etc.)
E_GEN_PREREQUISITES => 5, # Prereq OS pkgs are missing and can't be auto installed (likely Yum not setup)
E_GEN_BAD_ENV => 6, # Not root or not x64 arch or OS distribution not supported
E_GEN_DISK_SPACE => 7, # Not enough space on the target filesystem for the requested operation
E_GEN_PREREQ_NOT_COMPAT => 8, # One or more installed OS packages are incompatible with XMS prerequisites
E_UPG_NO_DOWNGRADE => 20, # Downgrade not allowed
E_UPG_SAME_VERSION => 21, # Upgrade version already installed
};
my $Errno = E_GEN_FAILED; # Examined when subs return false
#
# log management
#
use constant { LL_DEBUG => 6,
LL_VVERBOSE => 5,
LL_VERBOSE => 4,
LL_INFO => 3,
LL_WARNING => 2,
LL_ERROR => 1,
LL_NONE => 0
};
my $Xms_Log_File = 'xms_install.log';
my $Xms_Log_Level = LL_INFO;
#
# Firewall ports
#
my $Xms_Fw_Ports_T = 'tcp: 22, 80, 81, 443, 1080, 5060, 10443, 15001';
my $Xms_Fw_Ports_U = 'udp: 5060, 49152-53152, 57344-57840';
#
# service managment
#
my @OS_Service_Names = qw (ntpd httpd lighttpd snmpd snmptrapd);
my %OS_Services;
my %XMS_Service_Names = ( 'dlgclockdaemon' => 'HMP subsystem',
'media_server' => 'MSML Media Server (legacy)',
'nodecontroller' => 'Node Controller',
'broker' => 'Broker',
'xmsrest' => 'RESTful API',
'appmanager' => 'Application Manager',
'xmserver' => 'XM Server',
'httpclient' => 'HTTP Client',
'mrcpclient' => 'MRCP Client',
'netann' => 'NETANN Service',
'vxmlinterpreter' => 'VXML Interpreter',
'msmlserver' => 'MSML Service',
'rtcweb' => 'WebRTC Service',
'verification' => 'System Verification Server',
'snmpsubagent' => 'SNMP subagent'
);
my %XMS_Services;
my $Nodecontroller;
my $Hmp;
#
# command line options
#
my %CmdLnOpt = ( 'install' => 0,
'update' => 0,
'clean-install' => 0,
'remove' => 0,
'test' => 0,
'cfg-selinux' => undef,
'cfg-hosts' => undef,
'cfg-firewall' => undef,
'cfg-prereq ' => undef,
'cfg-https' => undef,
'yes' => 0,
'help' => 0,
'distdir' => '',
'log' => 1,
'logfile' => "xms_install.log",
'append' => 0,
'bn4k' => '',
'xms-cliport' => undef,
'xms-logdir' => '',
'xms-loglevel' => '',
'xms-rtpaddr' => '',
'xms-bindaddr' => '',
'xms_bindport' => undef,
'verbose' => LL_INFO,
'quiet' => 0,
);
#
# Miscellaneous
#
my $Conf_File_Backup_Ext = '.bak_xms';
# post install setup paths and files
my $Ssl_Conf_File = '/etc/httpd/conf.d/ssl.conf';
my $Ssl_Conf_File_Bk = $Ssl_Conf_File . $Conf_File_Backup_Ext;
# console output handle
my $ConFd = *STDOUT;
#********************************************
# Utility subs
#********************************************
sub usage {
my $help_text = <<END_HELP;
Usage: $0 [OPTION]...
Mode options:
-i, --install Install XMS if no previous version exists (default)
-u, --update Update XMS without affecting current configuration
-r, --remove Remove XMS
-t, --test Test system and report status without installing anything
Platform configuration options (negatable with --no-cfg-....):
--cfg-selinux Disable selinux (default:ask)
--cfg-hosts Configure /etc/hosts file (default:ask)
--cfg-prereq Install prerequisite OS packages (default:ask)
--cfg-https Backup and replace https settings (default:ask)
Advanced options
--xms-cliport PORT Use PORT for media processing sybsystem (default:23)
--xms-logdir DIR XMS log file directory
--xms-loglevel LVL Default XMS log level (ERROR,WARN,NOTICE,INFO,DEBUG - dflt: WARN)
--xms-rtpaddr ADR XMS IPv4 RTP address
--xms-bindaddr ADR XMS IPv4 bind address
--xms-bindport PORT XMS bind port
General options
-y, --yes Answer yes to all questions
-h, --help: Display this message and exit
-d, --distdir DIR Directory where the XMS distribution is located
-l, --log, --nolog Log (or not) results to a file (default:enabled)
-f, --logfile FILE Use FILE as the log filename (default: xms_install.log)
-a, --append Append to the log file if it exists (default:off)
-v, --verbose Print detailed progress information (-vv very verbose)
-q, --quiet Do not write anything to standard output
END_HELP
print STDERR $help_text;
}
#
# various system platform checks
#
sub is_distro_supported {
my %distros = @_;
my $dist_keys = join( '|', keys(%distros) );
# check for Red Hat / CentOS / Oracle
if ( open( my $distfh, "<", "/etc/system-release" ) ) {
my $line = <$distfh>;
chomp($line);
close($distfh);
$Distro_ID = $line;
if ( $line =~ /^\s*($dist_keys)\b/ ) {
my ( $maj, $min ) = split( '\.', $distros{$1} );
if ( $line =~ /\b($maj\.[$min-9])\b/ ) {
return 1;
}
}
}
else {
print "Could not open /etc/system-release:$! \n";
}
return;
}
sub is_distro {
my $distro = shift;
$Distro_ID or is_distro_supported();
return $Distro_ID =~ /$distro/;
}
sub is_selinux_enforcing {
if ( -x '/usr/sbin/getenforce' ) {
my $rc = `/usr/sbin/getenforce`;
return $rc =~ /^Enforcing/;
}
else {
return;
}
}
sub is_64bit_platform {
my $arch = `uname -i`;
return $arch =~ /^x86_64|amd64/;
}
sub is_root {
return $> == 0;
}
sub is_install_mode {
my $mode_mask = shift;
return $Xms_Install_Mode & $mode_mask;
}
sub is_yum_setup {
my $bin = `which yum`;
my @repos = @_;
state $Yummy = -1;
chomp($bin);
if ( $Yummy < 0 ) {
my $rc = 0;
if ( -x $bin ) {
my $cmd = join( ' ', $bin, "repolist enabled", @repos, "2> /dev/null" );
open( my $outp, "-|", $cmd );
foreach my $line (<$outp>) {
if ( $line =~ /^repolist:/ ) {
my ( $repo, $numb ) = split( ' ', $line, 2 );
chomp($numb);
$numb =~ s/,//;
$rc += $numb;
log_msg( LL_DEBUG, "Yum enabled repolist @repos status: $numb" );
}
}
close($outp);
}
$Yummy = $rc;
}
return $Yummy;
}
sub is_xms_installed {
return $Xms_Install_Status == XIS_INSTALLED;
}
sub is_installation_valid {
return $Xms_Install_Status != XIS_PARTIAL;
}
# get free disk space on root filesystem in MB
# returns -1 on error
sub get_disk_free_space {
my $rv = -1;
if ( open( my $output, 'df -m /usr 2>/dev/null | ' ) ) {
while (<$output>) {
my @fields = split(/\s+/);
if ( $fields[0] ne 'Filesystem' ) {
$rv = $fields[3];
}
}
close $output;
}
else {
log_msg( LL_WARNING, "Can't get free disk space: $!" );
}
return $rv;
}
#
# Check operating environment for minimum requirements
#
sub verify_env {
if ( !is_root() ) {
print "$0: XMS installation requires root priviledges.\n";
return;
}
if ( !is_64bit_platform() ) {
print "XMS requires a 64-bit platform.\nInstallation aborted.";
return;
}
if ( !is_distro_supported(%Distros_Supported) ) {
print "Detected unsupported Linux distribution or version", $Distro_ID ? ":\n$Distro_ID\n" : "\n";
return;
}
if ( !( -x '/etc/redhat-lsb/lsb_killproc' and -x '/etc/redhat-lsb/lsb_pidofproc' ) ) {
print "XMS requires Red Hat LSB (/etc/redhat-lsb/*) utilities\n";
return;
}
return 1;
}
# prompt user and obtain a response
# single character options only
sub ask_user {
my $prompt = shift; # the question
my $options = lc(shift); # the choices
my $default = lc(shift); # the default choice
my $response; # the user's response
#auto answer if requested
if ( $CmdLnOpt{'yes'} ) {
return $default;
}
my $disp_opt = join '/', map { $_ eq $default ? uc($_) : $_ } split( //, $options );
print $ConFd "$prompt [$disp_opt]: ";
while (1) {
$response = <STDIN>;
$response = lc($response);
chomp($response);
$response =~ s/^$/$default/;
last if ( $response =~ /^[$options]$/ );
print $ConFd "Invalid response. Try again [$disp_opt]: ";
}
return $response;
}
#
# Process command line options
#
sub process_options {
Getopt::Long::Configure("bundling");
my $rc = GetOptions( \%CmdLnOpt, 'install|i', 'update|u', 'remove|r', 'test|t', 'cfg-selinux!',
'cfg-hosts!', 'cfg-firewall!', 'cfg-prereq!', 'cfg-https!', 'xms-cliport=i', 'yes|y',
'help|h', 'distdir|d=s', 'log!', 'logfile|f=s', 'append|a', 'bn4k=s',
'xms-logdir=s', 'xms-loglevel=s', 'xms-rtpaddr=s', 'xms-bindaddr=s', 'xms-bindport=i', 'xms-optsrv=s',
'verbose|v+', 'quiet|q'
);
if ( !$rc or $#ARGV != -1 ) {
foreach (@ARGV) {
print "Unrecognized Parameter: $_\n";
}
print "Try $0 --help for more information.\n";
return;
}
if ( $CmdLnOpt{help} ) {
usage();
return;
}
# we can only operate in one mode at a time
if ( ( $CmdLnOpt{'install'} + $CmdLnOpt{'update'} + $CmdLnOpt{'remove'} + $CmdLnOpt{'test'} ) > 1 ) {
print "Operating mode option must be unique\n";
print "Try $0 --help for more information.\n";
return;
}
$Xms_Install_Mode = IM_INSTALL;
if ( $CmdLnOpt{'update'} ) { $Xms_Install_Mode = IM_UPDATE }
elsif ( $CmdLnOpt{'install'} ) { $Xms_Install_Mode = IM_INSTALL }
elsif ( $CmdLnOpt{'remove'} ) { $Xms_Install_Mode = IM_REMOVE }
elsif ( $CmdLnOpt{'test'} ) { $Xms_Install_Mode = IM_TEST }
$Xms_Log_File = $CmdLnOpt{logfile};
if ( $CmdLnOpt{'distdir'} ) { $Xms_Pkg_Dir = $CmdLnOpt{'distdir'}; }
$Xms_Log_Level = $CmdLnOpt{verbose} <= LL_DEBUG ? $CmdLnOpt{verbose} : LL_DEBUG;
#force fireweall to manual config for this release
$CmdLnOpt{'cfg-firewall'} = 0;
for my $prm ( 'xms-rtpaddr', 'xms-bindaddr' ) {
if ( $CmdLnOpt{$prm} and $CmdLnOpt{$prm} !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ ) {
print 'Value "', $CmdLnOpt{$prm}, '" invalid for option ', $prm, " (IPv4 address expected)\n";
return;
}
}
if ( $CmdLnOpt{'xms-rtpaddr'} and $Xms_Install_Mode != IM_INSTALL ) {
print "xms-rtpaddr option is available in installation mode only\n";
return;
}
for my $prm ( 'xms-cliport', 'xms-bindport' ) {
if ( defined( $CmdLnOpt{$prm} ) ) {
if ( $CmdLnOpt{$prm} < 1 or $CmdLnOpt{$prm} > 49151 ) {
print $prm, " value must be between 1 and 49151\n";
return;
}
}
else { $CmdLnOpt{$prm} = 0 }
}
if ( $CmdLnOpt{'bn4k'} ) {
if ( !-e $CmdLnOpt{'bn4k'} ) {
print "BN4K path $CmdLnOpt{'bn4k'} does not exist\n";
return;
}
elsif ( !pst_task_bn4k( $CmdLnOpt{'bn4k'}, 0 ) ) {
print "BN4K file verification failed\n";
return;
}
@OS_Service_Names = qw (ntpd lighttpd);
$CmdLnOpt{'cfg-https'} = 0;
}
if ( $CmdLnOpt{'xms-logdir'} ) {
if ( !File::Spec->file_name_is_absolute( $CmdLnOpt{'xms-logdir'} ) ) {
print "XMS log directory $CmdLnOpt{'xms-logdir'} is not an absolute path\n";
return;
}
$CmdLnOpt{'xms-logdir'} =~ s/\/$//;
if ( !-e $CmdLnOpt{'xms-logdir'} ) {
print "XMS log directory $CmdLnOpt{'xms-logdir'} does not exist\n";
return;
}
if ( !-d $CmdLnOpt{'xms-logdir'} ) {
print "XMS log directory $CmdLnOpt{'xms-logdir'} is not a directory\n";
return;
}
}
if ( $CmdLnOpt{'xms-loglevel'} and $CmdLnOpt{'xms-loglevel'} !~ /^\s*(ERROR|WARN|NOTICE|INFO|DEBUG)\s*/ ) {
print "XMS log level $CmdLnOpt{'xms-loglevel'} is not a recognized level. (Expected ERROR | WARN | NOTICE | INFO | DEBUG )\n";
return;
}
if ( $CmdLnOpt{'xms-optsrv'} ) {
my @services = split( ' ', $CmdLnOpt{'xms-optsrv'} );
#foreach (@services) { print $_ , "\n"};
if ( scalar @services > 1 and /^(ALL|NONE)/ ~~ @services ) {
print "Ambigous optional services request\n";
return;
}
}
# quiet mode implies a 'yes' answer to all questions
if ( $CmdLnOpt{'quiet'} ) {
$CmdLnOpt{'yes'} = 1;
}
if ( $CmdLnOpt{'quiet'} ) { open( $ConFd, '>', '/dev/null' ) }
return 1;
}
#
# signal handling
#
sub SIG_Cleanup_handler {
my ($sig) = @_;
# clean up , log message and close logs
log_msg( LL_WARNING, "Caught signal SIG$sig : Operation aborted." );
exit_cleanup(1);
}
sub SIG_Die_handler {
my ($msg) = @_;
# clean up , log message and close logs
log_msg( LL_ERROR, "Fatal Error: $msg" );
exit_cleanup(1);
}
sub SIG_Warn_handler {
my ($msg) = @_;
# clean up , log message and close logs
log_msg( LL_WARNING, "$msg" );
}
#
# logging
#
my $logged_errors = 0;
my $logged_warnings = 0;
my $Logger;
sub log_init {
eval {
require Log::Message;
Log::Message->import();
1;
}
or do {
my $error = $@;
print "\nThis script requires the perl Log::Message module (perl-Log-Message-*.x86_64.rpm) available in your Linux distribution\n";
exit 1;
};
if ( !( $Logger = Log::Message->new( private => 1, level => 'log' ) ) ) {
print "Error initializing log file";
return;
}
my $date = localtime();
$date =~ s/\d\d:\d\d:\d\d\s//;
$Logger->store( message => "XMS installation management started $date",
tag => LL_INFO,
level => 'log',
);
# verify that we can write the log file
if ( !File::Spec->file_name_is_absolute($Xms_Log_File) ) {
$Xms_Log_File = File::Spec->rel2abs($Xms_Log_File);
}
my ( $volume, $dir, $file ) = File::Spec->splitpath($Xms_Log_File);
if ( $CmdLnOpt{'log'} and !-w $dir ) {
print "Error: $dir is not writable\n";
return;
}
return 1;
}
# log a message to the message stack
# level : LL_xxxxx
#
my @level_name = qw( NONE ERROR WARN1 INFO1 INFO2 INFO3 DEBUG);
sub log_msg {
my $msg_tag = shift;
my $msg = join( '', @_ );
$Logger->store( message => $msg,
tag => $msg_tag,
level => 'log'
);
if ( $msg_tag <= $Xms_Log_Level ) {
print $ConFd $level_name[$msg_tag], ': ', $msg, "\n";
}
if ( $msg_tag == LL_ERROR ) { $logged_errors++ }
elsif ( $msg_tag == LL_WARNING ) { $logged_warnings++ }
}
sub log_close {
my @msg_list;
log_msg( LL_INFO, "Operation completed with $logged_errors errors and $logged_warnings warnings\n" );
if ( $logged_errors or $logged_warnings ) {
print $ConFd "INFO1: See $Xms_Log_File for details\n";
}
@msg_list = $Logger->flush();
if ( $CmdLnOpt{'log'} ) {
open( my $LOGFILE, $CmdLnOpt{'append'} ? '>>' : '>', $Xms_Log_File );
# log file is always at least verbose
foreach my $msg_item (@msg_list) {
if ( $msg_item->tag <= LL_VERBOSE or $msg_item->tag <= $Xms_Log_Level ) {
print $LOGFILE ( log_format_file_msg($msg_item) );
}
}
close($LOGFILE);
}
}
sub log_format_file_msg {
my $msg_item = shift;
my $timestamp = ( $msg_item->when =~ /(\d\d:\d\d:\d\d)/ ) ? $1 : '';
my $msg = join( ' ', $timestamp, $level_name[ $msg_item->tag ], " ", $msg_item->message, "\n" );
return $msg;
}
sub log_format_console_msg {
my $msg_item = shift;
my $msg = join( ' ', $level_name[ $msg_item->tag ] . ':', $msg_item->message, "\n" );
return $msg;
}
# prints messages to stdout that
# match the tag
sub log_print_con {
my $ltag = shift;
my @msg_list = $Logger->retrieve( tag => $ltag );
foreach my $msg_item (@msg_list) {
if ( $msg_item->tag <= $Xms_Log_Level ) {
print $ConFd log_format_console_msg($msg_item);
}
}
}
#
# Execute a command and log stderr
# log stdout at DEBUG log level
# return true if successfull
#
sub exec_log_stderr {
my $bin = shift;
my @params = @_;
my $rc;
my $tsof = '/dev/null';
$bin = `which $bin`;
chomp($bin);
if ( !-x $bin ) {
log_msg( LL_ERROR, "Can't locate executable: $bin" );
return;
}
my $tef = File::Temp->new( EXLOCK => 0 );
if ( $Xms_Log_Level == LL_DEBUG ) {
$tsof = File::Temp->new( EXLOCK => 0 );
}
# note: if selinux is enforcing , stderr is not redirected
my $cmd = join( ' ', $bin, @params, "2> $tef 1> $tsof" );
log_msg( LL_DEBUG, "exec_log_stderr cmd: $cmd" );
if ( $rc = system($cmd) ) {
if ( $rc == -1 ) {
log_msg( LL_ERROR, "Can't exec $cmd" );
}
else {
log_msg( LL_DEBUG, "exec_log_stderr: $cmd returned: ", $rc >> 8 );
}
}
if ($rc) {
while ( my $line = <$tef> ) {
chomp($line);
log_msg( LL_ERROR, $line );
}
}
if ( $Xms_Log_Level == LL_DEBUG ) {
while ( my $line = <$tsof> ) {
chomp($line);
if ($line) { log_msg( LL_DEBUG, $line ); }
}
}
return $rc == 0;
}
#
# Remove a directory tree from the file system
#
sub utl_remove_tree {
my $xdir = shift;
my $rc = 1;
if ( -e $xdir ) {
remove_tree( $xdir, { error => \my $errs } );
if (@$errs) {
for my $file_msg (@$errs) {
my ( $file, $msg ) = %$file_msg;
if ( $file eq '' ) {
log_msg( LL_ERROR, "Error removing $xdir: $msg" );
}
else {
log_msg( LL_ERROR, "Can't unlink $file: $msg" );
}
}
$rc = 0;
}
else {
log_msg( LL_VVERBOSE, "Removed dir: $xdir" );
}
}
return $rc;
}
#
# Operating system configuration tasks
#
# check or configure (disable) selinux for XMS
# check returns true if selinux is diabled in the config file
# and is currently not enforcing
#
sub cfg_selinux {
my $action = shift;
my $selconf = '/etc/selinux/config';
my $selconf_bak = $selconf . $Conf_File_Backup_Ext;
my $sel_filestatus = 0;
my $sel_status = 0;
# get selinux config file setting
if ( -e $selconf ) {
if ( open( my $selcnfh, '<', $selconf ) ) {
while ( my $line = <$selcnfh> ) {
next unless $line =~ /^\s*SELINUX\s*=\s*disabled/;
$sel_filestatus = 1;
last;
}
close($selcnfh);
}
else {
log_msg( LL_ERROR, "Could not open $selconf for reading" );
return $sel_filestatus;
}
# get current runtime setting as well
if ( -x '/usr/sbin/getenforce' ) {
my $enforce = `/usr/sbin/getenforce`;
$sel_status = $enforce =~ /^disabled|permissive/i;
}
else {
$sel_status = 1;
}
}
else {
return 1;
}
if ( $action == OSCA_CHECK ) {
return $sel_status && $sel_filestatus;
}
else {
# change current policy
if ( !$sel_status ) {
if ( system('/usr/sbin/setenforce 0') ) {
log_msg( LL_ERROR, "Could not turn SELINUX enforcing off" );
}
else {
$sel_status = 1;
}
}
if ( !$sel_filestatus ) {
if ( copy( $selconf, $selconf_bak ) ) {
if ( open( my $selcnfbakh, '<', $selconf_bak ) ) {
if ( open( my $selcnfh, '>', $selconf ) ) {
while ( my $line = <$selcnfbakh> ) {
if ( $line =~ s/^\s*SELINUX\s*=\s*\w+\b/SELINUX=disabled/ ) {
$sel_filestatus = 1;
}
print $selcnfh $line;
}
close($selcnfh);
}
else {
log_msg( LL_ERROR, "Can't open file $selconf for writing: $!" );
}
close($selcnfbakh);
}
else {
log_msg( LL_ERROR, "Can't open file $selconf_bak for reading: $!" );
}
}
else {
log_msg( LL_ERROR, "Can't back up file $selconf: $!" );
}
}
}
return $sel_status and $sel_filestatus;
}
# check status of ssl.conf or configure (backup ssl.conf)
# so that xms.conf can be used instead.
# check returns true ssl.conf does not exist
sub cfg_https {
my $action = shift;
if ( $action == OSCA_CHECK ) {
my $grp = $Pkg_Groups{'prereq'};
my $pkg = $grp->get_package('mod_ssl.x86_64');
return ( $pkg->is_installed() and !-e $Ssl_Conf_File );
}
elsif ( $action == OSCA_CONFIGURE ) {
if ( -e $Ssl_Conf_File ) {
if ( !move( $Ssl_Conf_File, $Ssl_Conf_File_Bk ) ) {
log_msg( LL_ERROR, "Can't rename $Ssl_Conf_File to $Ssl_Conf_File_Bk" );
}
else {
log_msg( LL_VERBOSE, "$Ssl_Conf_File renamed to $Ssl_Conf_File_Bk" );
return 1;
}
}
else { return 1; }
}
return;
}
# check status or configure fw ports
# todo future: implement some basic rule managment
#
sub cfg_firewall {
my $action = shift;
if ( $action == OSCA_CHECK ) {
# Not implemented : force to condition not satisfied
return;
}
elsif ( $action == OSCA_CONFIGURE ) {
# not implemented: force to failure
return;
}
return;
}
#
# Check and/or install prerequisite packages
#
sub cfg_os_pkg_prereq {
my $action = shift;
my $rc = 0;
my $grp = $Pkg_Groups{'prereq'};
my @missing_packages = $grp->get_not_installed();
if ( scalar @missing_packages == 0 ) {
return 1;
}
if ( $action == OSCA_CONFIGURE ) {
# handle libjpeg special case. If required , install from supplied packages before
# installing from repository to avoid libjpeg-turbo being installed instead.
if ( my $pkg = $grp->get_package('libjpeg.i686') ) {
if ( !$pkg->is_installed() ) {
$pkg->install();
$grp->refresh_status();
}
}