diff -ruN smeserver-affa-3.3.1.old/root/sbin/affa smeserver-affa-3.3.1/root/sbin/affa --- smeserver-affa-3.3.1.old/root/sbin/affa 2021-03-29 14:48:44.000000000 +0200 +++ smeserver-affa-3.3.1/root/sbin/affa 2021-09-21 17:09:30.045265189 +0200 @@ -11,8 +11,24 @@ # under certain conditions; type 'affa --license' for details. #---------------------------------------------------------------------- my $VERSION = '3.3.1'; + # This will remove the trailing -1 release number for docs -my $shortVersion =~ s/-[0-9]+// ; +my $shortVersion = $VERSION; +$shortVersion =~ s/-[0-9]+//; + + +#---------------------------------------------------------------------- +# +# A few notes - 21/09/2021 +# +# This is a huge script and probably needs breaking up +# It probably has lots that can be cleaned or removed +# It is starting to become systemd aware, but +# SME is still not fully systemd manageable and stil relies +# on some old init scripts +# +# I have built freedup for SME v10 in my reetp test repo +# No idea if it works #---------------------------------------------------------------------- use strict; @@ -38,6 +54,9 @@ # ajout pour V3.2.2-2 use File::Basename; +# Added for 3.3.1 +use esmith::Service; + #use Affa::lib qw(ExecCmd setlog lg dbg); ########use Affa::lib qw(ExecCmd setlog lg dbg); @@ -66,7 +85,7 @@ sub cleanup(); sub convertReportV2toV3($$); sub cronSetup(); -sub dbg($); +# sub dbg($); reetp - duplicate sub dbg($); sub deduplicate($); sub deleteJob(); @@ -101,7 +120,7 @@ sub killall(); sub killJob($); sub killProcessGroup($$); -sub lg($); +#sub lg($); reetp - duplicate sub lg($); sub listArchives(); sub listArchivesRaw($); @@ -159,22 +178,72 @@ ########## fin ajouts ################################################################################### +################################################################################### +################ ajouts + +my $configDB = esmith::ConfigDB->open or die "Error: Couldn't open config db."; +my $LocalIP = $configDB->get("LocalIP")->value; +my $DomainName = $configDB->get("DomainName")->value; + +my $rpmlist = "/home/e-smith/db/affa-rpmlist"; # list of installed RPMs + +my @services = ( 'crond', 'smbd', 'qmail', 'qpsmtpd', 'sqpsmtpd' ); + +my $ServicesStopped = 0; + +my $sshQuiet = "-q"; + +my $ServerBasename = "AFFA.$SystemName.$DomainName-$LocalIP"; + +############## fin ajouts +#################################################################################### + my $allow_retry = 0; my $defaultEmail = 'admin'; my $SystemName = hostname(); ( my $Domain = $SystemName ) =~ s/([^\.]*)\.//; + #################################################################################### -################################################début modifs +############# début modifs #my $hostname=$1; my $hostname = $SystemName; -################################################# fin modifs +############# fin modifs #################################################################################### -my $affaTitle = "Affa version $VERSION on $SystemName"; -my $smbconf = '/etc/samba/smb.conf'; -my $SambaStartScript = '/etc/init.d/smb'; -my $NRPEStartScript = '/etc/init.d/nrpe'; -my $dedupBinary = '/usr/bin/freedup'; + +my $affaTitle = "Affa version $VERSION on $SystemName"; +my $smbconf = '/etc/samba/smb.conf'; + +############# +# reetp +# So we could change this to check the samba status as this is ONLY checking +# For the script existence OR /sbin/e-smith/service smb status | grep Active + +my $systemD = new esmith::Service(); + +# Service command << not required now +# $serviceCommand = '/sbin/e-smith/service' + +my $SambaStartScript = '/usr/lib/systemd/system/smbd.service'; +#my $smbStatus = $configDB->get_prop( 'smb', 'status' ) || 'disabled'; + +$systemD->{'serviceName'} = 'smbd'; + +my $smbdStatus = ( $systemD->is_enabled() ) ? "enabled" : "disabled"; + +# nrpe too +my $NRPEStartScript = '/usr/lib/systemd/system/nrpe.service'; +#my $nrpeStatus = $configDB->get_prop( 'nrpe', 'status' ) || 'disabled'; + +$systemD->{'serviceName'} = 'nrpe'; + +my $nrpeStatus = ( $systemD->is_enabled() ) ? "enabled" : "disabled"; + +############# + +# Test binary built in reetpTest repo +# No idea if it works on v10 +my $dedupBinary = '/usr/bin/freedup'; my $cfg; # ini file @@ -189,33 +258,14 @@ my $interactive = 0; my %autoMounted = (); my $interrupt = ''; -my $ExecCmdOutout = ''; +my $ExecCmdOut = ''; my @Messages = (); # List of all log messages my $logdir = '/var/log/affa'; # Logdir my $logfile = "$logdir/affa-GLOBAL-LOGFILE.log"; # Logfile +my $scriptdir = '/etc/affa/scripts'; my $lockdir = '/var/lock/affa'; # Process lock File::Path::mkpath( $lockdir, 0, 0700 ) if not -d $lockdir; -my $scriptdir = '/etc/affa/scripts'; - -################################################################################### -################ ajouts - -my $config = esmith::ConfigDB->open or die "Error: Couldn't open config db."; -my $LocalIP = $config->get("LocalIP")->value; -my $DomainName = $config->get("DomainName")->value; - -my $rpmlist = "/home/e-smith/db/affa-rpmlist"; # list of installed RPMs -my @services = ( 'crond', 'smb', 'qmail', 'qpsmtpd', 'sqpsmtpd' ); - -my $ServicesStopped = 0; - -my $sshQuiet = "-q"; - -my $ServerBasename = "AFFA.$SystemName.$DomainName-$LocalIP"; - -############## fin ajouts -#################################################################################### if ( not $ARGV[0] ) { showHelp(1); @@ -614,7 +664,7 @@ "$job{'RootDir'}/$jobname/scheduled.running/" ); $status = ExecCmd( @cmd, 0 ); - $rsyncOutput = $ExecCmdOutout; + $rsyncOutput = $ExecCmdOut; } else # Standard Linux { @@ -664,7 +714,7 @@ lg("Running rsync..."); $status = ExecCmd( @cmd, 0 ); - $rsyncOutput = $ExecCmdOutout; + $rsyncOutput = $ExecCmdOut; } # if nothing was transferred, scheduled.running does not exist @@ -1091,7 +1141,7 @@ my @cmd = ( 'find', '/etc/affa/', '-type', 'f', '-name', '"*.conf"' ); ExecCmd( @cmd, 0 ); my @ret = (); - my @list = split( /[\r\n]/, $ExecCmdOutout ); + my @list = split( /[\r\n]/, $ExecCmdOut ); foreach my $s (@list) { my $c = Config::IniFiles->new( -file => $s ); if ( not $c ) { @@ -1305,8 +1355,8 @@ 'echo OK' ); ExecCmd( @cmd, 0 ); - chomp($ExecCmdOutout); - if ( $ExecCmdOutout ne "OK" ) { + chomp($ExecCmdOut); + if ( $ExecCmdOut ne "OK" ) { $status = -1; if ( !$silent ) { affaErrorExit( @@ -1809,8 +1859,8 @@ ); my $stat = ExecCmd( @cmd, 0 ); $exectime = time() - $exectime; - $ExecCmdOutout =~ s/\n//; - $ExecCmdOutout =~ /([0-9]+) files of ([0-9]+) replaced by links.*?replaced files was ([0-9]+) bytes/; + $ExecCmdOut =~ s/\n//; + $ExecCmdOut =~ /([0-9]+) files of ([0-9]+) replaced by links.*?replaced files was ([0-9]+) bytes/; my $replacedFiles = defined $1 ? $1 : -1; my $totalFiles = defined $2 ? $2 : -1; my $savedBytes = defined $3 ? $3 : -1; @@ -1858,6 +1908,10 @@ print "# NRPE is not installed on the Affa server\n"; return -1; } + if ( $nrpeStatus ne 'enabled' ) { + lg("NRPE service is not running.") + return; + } open( FO, ">$af" ); print FO "command[check_affa]=sudo /sbin/affa --nrpe\n"; print FO "command[affa_jobsnrpe]=sudo /sbin/affa --_jobsnrpe\n"; @@ -1899,7 +1953,7 @@ shift(@csv); my $h; -##### début modifs + ##### début modifs # ($h = sprintf "+-%076s-+\n", '-') =~ s/0/-/g; ( $h = sprintf "+-%0110s-+\n", '-' ) =~ s/0/-/g; @@ -3222,8 +3276,8 @@ $job{'remoteUser'} . '@' . $job{'remoteHostName'}, 'echo OK' ); ExecCmd( @cmd, 0 ); - chomp($ExecCmdOutout); - print( $ExecCmdOutout eq "OK" ? "ok\n" : "FAILED\n" ); + chomp($ExecCmdOut); + print( $ExecCmdOut eq "OK" ? "ok\n" : "FAILED\n" ); } } @@ -3565,6 +3619,7 @@ lg($txt); print "$txt\n"; } + #################################################################################### #### début des modifs: copié à partir de Affa V2 et ajouté "$jobname" à mailTestWatchdogRemote(); if ( $job{'Watchdog'} eq 'yes' and $job{'remoteHostName'} ne 'localhost' ) { @@ -3580,58 +3635,63 @@ sub setupSamba() { lg("Configuring Samba"); - if ( not -f $SambaStartScript ) { - lg("Samba service is not installed or not properly configured."); - return; + if ( $smbdStatus ne 'enabled' ) lg("Samba service is not installed or not properly configured."); + return; +} +my %inc; +foreach my $jobname ( $cfg->Sections() ) { + next if $jobname eq 'GlobalAffaConfig'; + my %job = getJobConfig($jobname); + if ( not $job{'_SambaValidUser'} and $job{'SambaShare'} eq 'yes' ) { + lg("Job $jobname: No Valid Users defined. Samba has been access disabled."); } - my %inc; - foreach my $jobname ( $cfg->Sections() ) { - next if $jobname eq 'GlobalAffaConfig'; - my %job = getJobConfig($jobname); - if ( not $job{'_SambaValidUser'} and $job{'SambaShare'} eq 'yes' ) { - lg("Job $jobname: No Valid Users defined. Samba has been access disabled."); - } - my $affasmb = "/etc/samba/Affa-Job-$jobname.conf"; - if ( $job{'SambaShare'} eq 'yes' and $job{'_SambaValidUser'} ) { - open( FD, ">$affasmb" ) or affaErrorExit("Could not create $affasmb"); - print FD "[$jobname]\n"; - print FD "path = $job{'RootDir'}/$jobname\n"; - print FD "comment = Affa archive: $job{'Description'}\n"; - print FD "valid users = $job{'_SambaValidUser'}\n"; - print FD "force user = root\n"; - print FD "read only = yes\n"; - print FD "writable = no\n"; - print FD "veto files = /.$jobname-setup.ini/,/.doneDates/,/.AFFA3-REPORT/,/.AFFA-REPORT/.AFFA-TRASH/\n\n"; - close(FD); - $inc{"include = $affasmb"} = 1; - } - else { - unlink($affasmb); - } + my $affasmb = "/etc/samba/Affa-Job-$jobname.conf"; + if ( $job{'SambaShare'} eq 'yes' and $job{'_SambaValidUser'} ) { + open( FD, ">$affasmb" ) or affaErrorExit("Could not create $affasmb"); + print FD "[$jobname]\n"; + print FD "path = $job{'RootDir'}/$jobname\n"; + print FD "comment = Affa archive: $job{'Description'}\n"; + print FD "valid users = $job{'_SambaValidUser'}\n"; + print FD "force user = root\n"; + print FD "read only = yes\n"; + print FD "writable = no\n"; + print FD "veto files = /.$jobname-setup.ini/,/.doneDates/,/.AFFA3-REPORT/,/.AFFA-REPORT/.AFFA-TRASH/\n\n"; + close(FD); + $inc{"include = $affasmb"} = 1; + } + else { + unlink($affasmb); } +} - open( FD, "<$smbconf" ) or affaErrorExit("Could not open $smbconf"); - open( FW, ">$smbconf.$$" ) or affaErrorExit("Could not create $smbconf.$$"); - while () { - $_ = trim($_); - if ( $_ =~ /include = (\/etc\/samba\/Affa-Job.*conf)/ and not $inc{$_} ) { - unlink $1; - } - print FW "$_\n" if not $_ =~ /(include = \/etc\/samba\/Affa-Job|# Affa archives. Updated on)/; +open( FD, "<$smbconf" ) or affaErrorExit("Could not open $smbconf"); +open( FW, ">$smbconf.$$" ) or affaErrorExit("Could not create $smbconf.$$"); +while () { + $_ = trim($_); + if ( $_ =~ /include = (\/etc\/samba\/Affa-Job.*conf)/ and not $inc{$_} ) { + unlink $1; + } + print FW "$_\n" if not $_ =~ /(include = \/etc\/samba\/Affa-Job|# Affa archives. Updated on)/; - # ligne d'origine - Bug 9298 - if( $_ =~ /^\[global\]$/i ) { - if ( $_ =~ /^#\[Affa-jobs\]$/i ) { - printf FW "# Affa archives. Updated on " - . formatHTime( Date::Format::time2str( "%Y%m%d%H%M", time() ) ) . "\n"; - print FW join( "\n", sort keys %inc ) . "\n"; - } + # ligne d'origine - Bug 9298 - if( $_ =~ /^\[global\]$/i ) { + if ( $_ =~ /^#\[Affa-jobs\]$/i ) { + printf FW "# Affa archives. Updated on " . formatHTime( Date::Format::time2str( "%Y%m%d%H%M", time() ) ) . "\n"; + print FW join( "\n", sort keys %inc ) . "\n"; } - close(FD); - close(FW); +} +close(FD); +close(FW); + +rename( "$smbconf.$$", "$smbconf" ) or affaErrorExit("Moving $smbconf.$$ to $smbconf failed"); +#my @cmd = ( $servicecommand, 'smb', 'reload' ); # reetp changed to restart +#ExecCmd( @cmd, 0 ); # Reload Samba config + +$systemD->{'serviceName'} = 'smbd'; + +my $smbdStatus = ( $systemD->reload() ); # reetp - failure?? + +print "Status $smbdStatus\n"; - rename( "$smbconf.$$", "$smbconf" ) or affaErrorExit("Moving $smbconf.$$ to $smbconf failed"); - my @cmd = ( $SambaStartScript, 'reload' ); - ExecCmd( @cmd, 0 ); # Reload Samba config } sub sendErrorMesssage() { @@ -3937,7 +3997,7 @@ ################################################################################################################################################# ###################################################### début modifs ajouté comme dans Affa2 - startServices() if $ServicesStopped; + startServices() if $ServicesStopped; # reetp - note to self ###################################################### fin modifs #################################################################################################################################################### @@ -4009,13 +4069,13 @@ $forcelog = 1 if $job{'Debug'}; die "Fork failed: $!\n" unless defined( my $pid = open( RCHILD, "-|" ) ); if ($pid) { - $ExecCmdOutout = ''; + $ExecCmdOut = ''; while () { chomp($_); next if $_ eq ''; dbg("Exec Out: $pipestatus") if $forcelog and $pipestatus; # one step offset s/\e\[[0-9\;]+[A-Za-z]//g; # remove ANSI escape sequences - $ExecCmdOutout .= "$_\n"; + $ExecCmdOut .= "$_\n"; $pipestatus = $_; } close(RCHILD); @@ -4024,7 +4084,7 @@ dbg("Exec Cmd: @cmd"); exec("@cmd 2>&1; echo \${PIPESTATUS}") or die "exec failed: $!\n"; } - $ExecCmdOutout =~ s/$pipestatus\n$//; + $ExecCmdOut =~ s/$pipestatus\n$//; $pipestatus = $? if not $pipestatus; dbg("Exec Out: exitstatus=$pipestatus") if $forcelog; return $pipestatus; @@ -4128,7 +4188,7 @@ $SIG{'INT'} = sub { }; $interactive = 1; my @cmd; - my $bootstrap = $config->get_prop( "bootstrap-console", "Run" ); + my $bootstrap = $configDB->get_prop( "bootstrap-console", "Run" ); my $jobname = $ARGV[0] || ''; my $archive = $ARGV[1] || 'scheduled.0'; #################################################################################### @@ -4235,11 +4295,12 @@ @cmd = ( "/sbin/e-smith/signal-event", "pre-restore" ); ExecCmd( @cmd, 0 ); - $config = esmith::ConfigDB->close; + $configDB = esmith::ConfigDB->close; ##### ligne d'origine AffaV2: runRiseRsync("$job{'RootDir'}/$jobname/$archive/", "/"); runRiseRsync( $jobname, "$job{'RootDir'}/$jobname/$archive/", "/" ); - $config = esmith::ConfigDB->open or die "Error: Couldn't open config db."; + + $configDB = esmith::ConfigDB->open or die "Error: Couldn't open config db."; imapIndexFilesDelete(); @@ -4254,13 +4315,13 @@ my $srcconfigPath = "/var/affa/$ServerBasename/scheduled.0/home/e-smith/db/configuration"; my $srcconfig = esmith::ConfigDB->open_ro($srcconfigPath) or affaErrorExit("Couldn't open source config db $srcconfigPath"); - $config->set_prop( "EthernetDriver1", "type", $srcconfig->get("EthernetDriver1")->value ); - $config->set_prop( "EthernetDriver2", "type", $srcconfig->get("EthernetDriver2")->value ); - $config->set_prop( "InternalInterface", "Driver", $srcconfig->get_prop( "InternalInterface", "Driver" ) ); - $config->set_prop( "InternalInterface", "Name", $srcconfig->get_prop( "InternalInterface", "Name" ) ); - $config->set_prop( "InternalInterface", "NICBondingOptions", + $configDB->set_prop( "EthernetDriver1", "type", $srcconfig->get("EthernetDriver1")->value ); + $configDB->set_prop( "EthernetDriver2", "type", $srcconfig->get("EthernetDriver2")->value ); + $configDB->set_prop( "InternalInterface", "Driver", $srcconfig->get_prop( "InternalInterface", "Driver" ) ); + $configDB->set_prop( "InternalInterface", "Name", $srcconfig->get_prop( "InternalInterface", "Name" ) ); + $configDB->set_prop( "InternalInterface", "NICBondingOptions", $srcconfig->get_prop( "InternalInterface", "NICBondingOptions" ) ); - $config->set_prop( "InternalInterface", "NICBonding", + $configDB->set_prop( "InternalInterface", "NICBonding", $srcconfig->get_prop( "InternalInterface", "NICBonding" ) || 'disabled' ); print "Please make sure that the server '" . $job{'remoteHostName'} . "' is not connected to your network.\n"; @@ -4313,11 +4374,11 @@ sub runRiseRsync($$$) { ( my $jobname, my $archive, my $dest ) = @_; -##### ligne d'origine AffaV2: (my $archive,my $dest)=@_; +#### ligne d'origine AffaV2: (my $archive,my $dest)=@_; my $b = new esmith::Backup or die "Error: Couldn't create Backup object\n"; -#### ligne d'origine AffaV2: my @SourceDirs = $opts{'all'} ? getSourceDirs() : $b->restore_list; +#### ligne d'origine AffaV2: my @SourceDirs = $opts{'all'} ? getSourceDirs() : $b->restore_list; my @SourceDirs = $opts{'all'} ? getSourceDirs($jobname) : $b->restore_list; if ( $jobname eq 'undo-rise' ) { @@ -4341,8 +4402,8 @@ my $status = ExecCmd( @cmd, 0 ); if ( $status == 0 or $status == 23 or $status == 24 ) { - # $Affa::lib::ExecCmdOutout =~ /(Total file size: [0-9]* bytes)/gm; - $ExecCmdOutout =~ /(Total file size: [0-9]* bytes)/gm; + # $Affa::lib::ExecCmdOut =~ /(Total file size: [0-9]* bytes)/gm; + $ExecCmdOut =~ /(Total file size: [0-9]* bytes)/gm; # print "OK. $1\n"; ##### Bug 9139: valeur de $1 non définie. print "OK. \n"; @@ -4355,26 +4416,42 @@ sub stopServices() { lg("Stopping services..."); - my @cmd; + + # my @cmd; + foreach my $svc (@services) { - @cmd = ( '/sbin/e-smith/service', $svc, 'stop' ); - ExecCmd( @cmd, 0 ); - - # print $Affa::lib::ExecCmdOutout; - print $ExecCmdOutout; + + #reetp stop services + $systemD->{'serviceName'} = $svc; + my $Status = ( $systemD->stop() ); + + # @cmd = ( $serviceCommand, $svc, 'stop' ); + # ExecCmd( @cmd, 0 ); + + # print $Affa::lib::ExecCmdOut; + # print $ExecCmdOut; + print "Start $svc $Status\n"; #reetp - status? } $ServicesStopped = 1; } sub startServices() { lg("Starting services..."); - my @cmd; + + #my @cmd; + foreach my $svc (@services) { - @cmd = ( '/sbin/e-smith/service', $svc, 'start' ); - ExecCmd( @cmd, 0 ); - - # print $Affa::lib::ExecCmdOutout; - print $ExecCmdOutout; + + + $systemD->{'serviceName'} = $svc; + my $Status = ( $systemD->start() ); + + # @cmd = ( $serviceCommand, $svc, 'start' ); + # ExecCmd( @cmd, 0 ); + + # print $Affa::lib::ExecCmdOut; + # print $ExecCmdOut; + print "Stop $svc $Status\n"; #reetp - status? } $ServicesStopped = 0; }