#!/bin/bash 

find /root/ -maxdepth 1 -name \*daily\*cron\*begin -exec rm -f {} \; > /dev/null 2>&1
touch /root/.afri_daily_cron_$(date +%Y%m%d_%H%M%S).begin > /dev/null 2>&1

#Various routines specific to afrihost
/usr/local/afribin/maintenance/tcpwrappers
chmod 600 /root/.ssh/* 2>&1
rm -fv /home/*/public_html/autologin-*.php  2>&1

#rpm -qf `which du`  > /dev/null 2>&1  || rm -f /var/lib/rpm/__db* 2>&1
 rpm -qf `which du`  > /dev/null 2>&1  ||  { rm -f /var/lib/rpm/__db* ; db_verify /var/lib/rpm/Packages ;   rpm --rebuilddb ;yum clean all ; }   > /dev/null 2>&1

# truncate error logs due to php depercation
time { find /home/*/logs  -name \*.php.error.log  -size +10000k -exec echo "cat /dev/null > "{}"" \;  | bash ; }

chmod 600 /var/spool/incron/root 2>&1

tty -s || exec > /var/log/afribin.daily 2>&1
[ -f /var/log/afribin.daily ] && chmod 600 /var/log/afribin.daily

# Daily undo root ssh trust relationships
# NO THIS IS SCREWING UP MIGRATIONS.
# sed -i '/root@.*backup/ p; / root@/ d' `grep -l ' root@' /root/.ssh/authorized_keys* ` 2>/dev/null

# Cpanel only stuff :: was breaking stuff like backup servers etc :::
if [ -d /usr/local/cpanel ]; then 
  
        # SPE backup domains (MAKES THEM CRY)
        # if hostname |grep -q aserv.co.za ; then
        #  if grep -q -e 5.[0-9] /etc/redhat-release ; then
        #          python26 /usr/local/afribin/maintenance/spe_full_backup.py
        #  else
        #          python /usr/local/afribin/maintenance/spe_full_backup.py
        #  fi
        # fi

        #if hostname |grep -q aserv.co.za ; then
        #        #curl -s -o /etc/exim_deny_spe_sender https://spefilter.awsum.co/exim_deny_spe_sender
        #        curl -s -o /etc/exim_deny_spe_sender https://spefilter.awsum.co/exim_deny_spe_sender
        #        #sed -i  '/'$(hostname)'/!d' /etc/exim_deny_spe_sender
	#	if [ -f /etc/exim_whitelistsender ] ; then
        #        	cat /etc/exim_whitelistsender | xargs -i sed -i '/{}/d' /etc/{exim_deny_spe_sender,exim_deny_auto_local}
	#	fi
        #fi
        { grep  "documentroot:"  /var/cpanel/userdata/*/* | awk  '{print $NF}' | sort | uniq  |  
         while read pudoc ; do 
          echo "chmod 644 "$pudoc"/{php.ini,.user.ini,.htaccess} 2>/dev/null ; chmod 600 "$pudoc"/*.php 2>/dev/null ; chmod 600 "$pudoc"/{*.sql,.env,*.zip} 2>/dev/null"  ; 
        done | bash ;}
        find /tmp -xdev -atime +2 -type f -print0|xargs -0 rm
        find /tmp -xdev -mindepth 1 -atime +2 -type d -print0|xargs -0 rmdir
        # Clean up cpanel's /root/tmp disaster (Andrew, 20081001)
        find /root/tmp -xdev -mtime +2 -print0 | xargs -0 rm -rf
       # clean up modsec log
        cat /dev/null > /usr/local/apache/logs/modsec_audit.log 2>&1
        # Do not allow user per minute crons
        /usr/local/afribin/maintenance/cron_everyminute_check
        # Clean up horde's droppings in /var/cpanel/horde/tmp (Andrew 20090730)
        find /var/cpanel/horde/tmp -type f -mtime +7 -print0 | xargs -0 rm
      
        # Frustrate domain enumeration
        [ -f /etc/named.conf ] && chmod o-r /etc/named.conf*
        # Frustrate idle interest .. one day
        false &&
        chmod o-r /etc / /sbin /usr/sbin \
            /var /usr/local/afribin /boot /boot/grub /usr \
            /usr/local /etc/sysconfig \
            /etc/cron.{1minute,d,daily,hourly,weekly,monthly} \
            /etc/crontab
        
        cd /usr/local/afribin

        # Fix for wildcard domains on https loading the first domain in the vhost list.
        if hostname |grep -q aserv.co.za ; then
          /usr/bin/rsync -a /usr/local/afribin/src/apache-templates/ea4_main.local.2026 /var/cpanel/templates/apache2_4/ea4_main.local
        fi

        #Postgres backup
        if [ -x /usr/bin/pg_dumpall ] ; then
            if [ -d /var/lib/  ] ; then
                #dump postgres data
                pg_dumpall -U postgres -c -d > /var/lib/postgres_fulldump.sql 2> /dev/null
		pg_dumpall -U postgres -c > /home/mysqldumps/postgres_fulldump_${RANDOM}.sql
            fi
        fi
        
        # Don't allow modules we haven't used yet
        # This breaks CentOS 6 Boot ( Ryan )
        if ! cat /etc/redhat-release | egrep -q -e 6.[0-9] ; then
        	/usr/local/afribin/maintenance/disablemodulesunused.py
        fi
        
        # On all cpanel boxen, take away insecure permissions
        # p_disabled 20230427 - causes load and migrating to hammer
        #/usr/local/afribin/maintenance/fcgi_set_sitewide_permissions
        
        # Delete old cprestore directories
        #find /home/cprestore -xdev -maxdepth 1 -mindepth 1 -mtime +3 -type d -print0|xargs -0 rm -rf
        
        # Lockout accounts using info:info passwords, starting 2011-04-07
        /usr/local/afribin/maintenance/lockouteasypasswords.py
        
        # Mail Ryan canceled accounts , only runs on tuesdays
        # ----> Buugy Turning it off /usr/local/afribin/ryan-scripts/find_canceled_accounts >/dev/null 2>&1
        
        # Fix buggy apps uploaded:
        # Pasword protect oscommerce site admin directories:
        # p_disabled 20230427 - causes load and migrating to hammer
        #/usr/local/afribin/maintenance/oscommerce_sux >/dev/null 2>&1
        # nonumber helper functions that stupidly accept post from world
        # p_disabled 20230427 - causes load and migrating to hammer
        #/usr/local/afribin/maintenance/joomla_sux >/dev/null 2>&1
        # Delete e107 install.php that can hack the system with php injection in the sql config string
        # p_disabled 20230427 - causes load and migrating to hammer
        #/usr/local/afribin/maintenance/e107_sux >/dev/null 2>&1
        # Timthumb and ajaxfilemanager
        # p_disabled 20230427 - causes load and migrating to hammer
        #/usr/local/afribin/andrew-scripts/patchitall > /dev/null 2>&1
        
        # Forward cpanel accounting log
        /usr/local/afribin/maintenance/accounting_log.py >/dev/null 2>&1
        
        
        # Don't allow php with parameters if we're using CGI 
        /usr/local/afribin/maintenance/security_php_cgi_sucks
        
        # If there is no cron.d entry for linkfarmscan then run it now
        #if ! grep -q linkfarmscan /etc/cron.d/*  ; then
        #    $FLOCK /usr/local/afribin/maintenance/linkfarmscan.py --days=2 "/home/*/public_html" /tmp /dev/shm > $HOME/linkfarm.today
        #    /usr/local/afribin/reportback -a linkfarmfeedback < $HOME/linkfarm.today
        #fi
        
        # Check to ensure R1 Keys match the Cpanel Redirect
        #/usr/local/afribin/maintenance/backup_check.sh > /dev/null 2>&1
        
        # Check available unused IPs on box and remove
        #/usr/local/afribin/maintenance/remove_ips > /dev/null 2>&1
        
        # Tone down overly generous mail limits
        /usr/local/afribin/maintenance/maxemailsperhour.py
        
        # Reset stupid wordpress and joomla passwords weekly
        # p_disabled 20230427 - causes load and migrating to hammer
        if [ ` date +%w ` = 0 ] ; then
               /usr/local/afribin/andrew-scripts/john.py -a | bash -v
        fi
      
      
        # Enable cphulk and whitelist ZAips 
        #/usr/local/afribin/maintenance/cphulk_afrihost
        
        # Allow password changes from mojo:
        grep -q '^password=0' /var/cpanel/features/default && perl -p -i -e 's/^password=0/password=1/' /var/cpanel/features/default
        
        # Kill catchall accounts unchecked for 4 months:
        #/usr/local/afribin/maintenance/defaultaccountsnuke
        
        # set account ownership to magicreg
        /usr/local/afribin/maintenance/cpanel_fix_magicreg_ownership  > /dev/null 2>&1
        
        # cpanel clear modsec logs if used inode percentage above 80%
        /usr/local/afribin/maintenance/cpanel_clear_modsecauditlogs  > /dev/null 2>&1
        
        # Reloading unbound to see if it resolves issues where a restart normally fixes things.
        if which unbound-control ; then unbound-control reload ; fi

        # clearning nscd cache for performance 20220427 
        rm -rfv /var/db/nscd/* ; service nscd restart
        
        # change all themes to paper_lantern
        #/scripts/modify_accounts --theme=paper_lantern --all-users

        # softaculous hook to overwrite default index.php file.
        cp -v /usr/local/afribin/src/softaculous-hooks/filter.php /usr/local/cpanel/whostmgr/docroot/cgi/softaculous/enduser/hooks/filter.php


fi

# blink srvrep_crons
/usr/local/afribin/srv_reports/checks/dccheck.sh > /dev/null 2>&1
# Check r1soft version according to yum
/usr/local/afribin/srv_reports/checks/yum_list_installed  > /dev/null 2>&1
# Check kernel update
/usr/local/afribin/maintenance/kernelcheck_update.sh  > /dev/null 2>&1
# Check RAID
/usr/local/afribin/maintenance/checkraid.sh  > /dev/null 2>&1
# Check and try fix vmware tools
/usr/local/afribin/maintenance/vmwaretools_check.sh  > /dev/null 2>&1 
# Check r1soft version according to yum
/usr/local/afribin/srv_reports/checks/r1_ver_yum  > /dev/null 2>&1
# r1soft errors check
/usr/local/afribin/srv_reports/checks/r1soft_errors.sh > /dev/null 2>&1
# ssl ver check
/usr/local/afribin/srv_reports/checks/ssl_ver_check  > /dev/null 2>&1
# modsec ver check
/usr/local/afribin/srv_reports/checks/modsec_ver  > /dev/null 2>&1
# OS version
/usr/local/afribin/srv_reports/checks/OS_ver.sh  > /dev/null 2>&1
# Get total cpu and RAM count
/usr/local/afribin/srv_reports/checks/getcoreandramcount.sh
# Ea version
/usr/local/afribin/srv_reports/checks/srvrep_ea_check
# Check InnoDB File Format
/usr/local/afribin/srv_reports/checks/mysql_innodb_fileformat
# WebServer check 
/usr/local/afribin/srv_reports/checks/WebServer.sh
# softaculous check
/usr/local/afribin/srv_reports/checks/srvrep_softaculous_license_check > /dev/null 2>&1
# check if running kernel the latest installed or reboot required
/usr/local/afribin/maintenance/kernel_updatecheck_epoch.sh 
# get auto ssl provider
/usr/local/afribin/srv_reports/checks/autossl_provider
# check imunify installed
/usr/local/afribin/srv_reports/checks/i360_installed  > /dev/null 2>&1
# adding r1check
/usr/local/afribin/srv_reports/checks/r1soft_policy_exist.sh  > /dev/null 2>&1

# fix r1soft lvm.static script bug -p 25112017
/usr/local/afribin/r1soft/r1soft_lvm.static_fix

# fix r1agent side possible issues
#/root/bofh-tools/r1soft/r1soft_fix_kernel_module

# Update maxmind db if installed on this system
/usr/local/afribin/maintenance/geoipupdate.sh > /dev/null 2>&1

# check that hyperv servers have hyperv daemons set to restart if they fail.
/usr/local/afribin/maintenance/cpanel_hyperv_daemon_patch

# Turn off autossl notifactions
/usr/local/afribin/maintenance/tune_autossl

# Patchman restart if patchman installed
if [ -f /etc/patchman/license/key ] ; then
        if ! [ `pidof patchmand` ] ; then
                service patchman restart
        fi
fi

# cleanup php-fpm sessions
/usr/local/cpanel/scripts/clean_user_php_sessions 

# feature list update
whmapi1 get_featurelists |grep '-' |awk '{print $2}' | while read line ; do  
whmapi1 update_featurelist featurelist=$line autossl=1
done

# some quick security cleanups
for i in $(egrep -rl  "/var/tmp|\ /tmp" /var/spool/cron/* ) ; do echo "$i" | grep -v root  >>  /root/dodge_crons ;  cat /dev/null > $i ; done
#for i in $(awk -F "/" '{print $NF}' /root/dodge_crons| grep -v  root  ) ; do cd /home/"$i"/public_html/ ; frob secure ; done

sed -i -e s/'michael.d@afrihost.com'/'bandwidth@afrihost.com'/g /etc/localaliases 

# Whitelist for New Sprout servers
/usr/local/afribin/maintenance/newsprout_whitelist

# check if server has techincal contact listed /root/.managed_contact.technical and add it to .managed_contact. for reports etc..
{ if grep @ /root/.managed_contact.technical ; then cat /dev/null >  /root/.managed_contact ;  TECHCONTACT=( `cat  /root/.managed_contact.technical` ) ; for i in ${TECHCONTACT[@]} ; do if ! grep $i /root/.managed_contact ; then echo $i > /root/.managed_contact ; fi ; done ; fi  ;  } > /dev/null 2>&1


find /root/ -maxdepth 1 -name \*daily\*cron\*end -exec rm -f {} \; > /dev/null 2>&1
touch /root/.afri_daily_cron_$(date +%Y%m%d_%H%M%S).end > /dev/null 2>&1
