"Fossies" - the Fresh Open Source Software Archive

Member "fogproject-1.5.9/lib/common/functions.sh" (13 Sep 2020, 139058 Bytes) of package /linux/misc/fogproject-1.5.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "functions.sh": 1.5.8_vs_1.5.9.

A hint: This file contains one or more very long lines, so maybe it is better readable using the pure text view mode that shows the contents as wrapped lines within the browser window.


    1 #!/bin/bash
    2 #
    3 #  FOG - Free, Open-Source Ghost is a computer imaging solution.
    4 #  Copyright (C) 2007  Chuck Syperski & Jian Zhang
    5 #
    6 #   This program is free software: you can redistribute it and/or modify
    7 #   it under the terms of the GNU General Public License as published by
    8 #   the Free Software Foundation, either version 3 of the License, or
    9 #    any later version.
   10 #
   11 #   This program is distributed in the hope that it will be useful,
   12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 #   GNU General Public License for more details.
   15 #
   16 #   You should have received a copy of the GNU General Public License
   17 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18 #
   19 dots() {
   20     local pad=$(printf "%0.1s" "."{1..60})
   21     printf " * %s%*.*s" "$1" 0 $((60-${#1})) "$pad"
   22     return 0
   23 }
   24 backupReports() {
   25     dots "Backing up user reports"
   26     [[ ! -d ../rpttmp/ ]] && mkdir ../rpttmp/ >>$workingdir/error_logs/fog_error_${version}.log
   27     [[ -d $webdirdest/management/reports/ ]] && cp -a $webdirdest/management/reports/* ../rpttmp/ >>$workingdir/error_logs/fog_error_${version}.log
   28     echo "Done"
   29     return 0
   30 }
   31 checkDatabaseConnection() {
   32     dots "Checking connection to master database"
   33     [[ -n $snmysqlhost ]] && host="--host=$snmysqlhost"
   34     sqloptionsuser="${host} -s --user=${snmysqluser}"
   35     mysql $sqloptionsuser --password="${snmysqlpass}" --execute="quit" >/dev/null 2>&1
   36     errorStat $?
   37 }
   38 registerStorageNode() {
   39     [[ -z $webroot ]] && webroot="/"
   40     dots "Checking if this node is registered"
   41     storageNodeExists=$(wget --no-check-certificate -qO - ${httpproto}://$ipaddress/${webroot}/maintenance/check_node_exists.php --post-data="ip=${ipaddress}")
   42     echo "Done"
   43     if [[ $storageNodeExists != exists ]]; then
   44         [[ -z $maxClients ]] && maxClients=10
   45         dots "Node being registered"
   46         curl -s -k -X POST -d "newNode" -d "name=$(echo -n $ipaddress|base64)" -d "path=$(echo -n $storageLocation|base64)" -d "ftppath=$(echo -n $storageLocation|base64)" -d "snapinpath=$(echo -n $snapindir|base64)" -d "sslpath=$(echo -n $sslpath|base64)" -d "ip=$(echo -n $ipaddress|base64)" -d "maxClients=$(echo -n $maxClients|base64)" -d "user=$(echo -n $username|base64)" --data-urlencode "pass=$(echo -n $password|base64)" -d "interface=$(echo -n $interface|base64)" -d "bandwidth=1" -d "webroot=$(echo -n $webroot|base64)" -d "fogverified" $httpproto://$ipaddress/${webroot}/maintenance/create_update_node.php
   47         echo "Done"
   48     else
   49         echo " * Node is registered"
   50     fi
   51 }
   52 updateStorageNodeCredentials() {
   53     [[ -z $webroot ]] && webroot="/"
   54     dots "Ensuring node username and passwords match"
   55     curl -s -k -X POST -d "nodePass" -d "ip=$(echo -n $ipaddress|base64)" -d "user=$(echo -n $username|base64)" --data-urlencode "pass=$(echo -n $password|base64)" -d "fogverified" $httpproto://$ipaddress${webroot}maintenance/create_update_node.php
   56     echo "Done"
   57 }
   58 backupDB() {
   59     dots "Backing up database"
   60     if [[ -d $backupPath/fog_web_${version}.BACKUP ]]; then
   61         [[ ! -d $backupPath/fogDBbackups ]] && mkdir -p $backupPath/fogDBbackups >>$workingdir/error_logs/fog_error_${version}.log 2>&1
   62         wget --no-check-certificate -O $backupPath/fogDBbackups/fog_sql_${version}_$(date +"%Y%m%d_%I%M%S").sql "${httpproto}://$ipaddress/$webroot/maintenance/backup_db.php" --post-data="type=sql&fogajaxonly=1" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
   63     fi
   64     if [[ $? -ne 0 ]]; then
   65         echo "Failed"
   66         if [[ -z $autoaccept ]]; then
   67             echo
   68             echo "   We were not able to backup the current database! Just press"
   69             echo "   [Enter] to proceed anyway or Ctrl+C to stop the installer."
   70             read
   71         fi
   72     else
   73         echo "Done"
   74     fi
   75 }
   76 updateDB() {
   77     case $dbupdate in
   78         [Yy]|[Yy][Ee][Ss])
   79             dots "Updating Database"
   80             local replace='s/[]"\/$&*.^|[]/\\&/g'
   81             local escstorageLocation=$(echo $storageLocation | sed -e $replace)
   82             sed -i -e "s/'\/images\/'/'$escstorageLocation'/g" $webdirdest/commons/schema.php
   83             wget --no-check-certificate -qO - --post-data="confirm&fogverified" --no-proxy ${httpproto}://${ipaddress}/${webroot}management/index.php?node=schema >>$workingdir/error_logs/fog_error_${version}.log 2>&1
   84             errorStat $?
   85             ;;
   86         *)
   87             echo
   88             echo " * You still need to install/update your database schema."
   89             echo " * This can be done by opening a web browser and going to:"
   90             echo
   91             echo "   $httpproto://${ipaddress}/fog/management"
   92             echo
   93             read -p " * Press [Enter] key when database is updated/installed."
   94             echo
   95             ;;
   96     esac
   97     dots "Update fogstorage database password"
   98     mysql $sqloptionsuser --password="${snmysqlpass}" --execute="INSERT INTO globalSettings (settingKey, settingDesc, settingValue, settingCategory) VALUES ('FOG_STORAGENODE_MYSQLPASS', 'This setting defines the password the storage nodes should use to connect to the fog server.', \"$snmysqlstoragepass\", 'FOG Storage Nodes') ON DUPLICATE KEY UPDATE settingValue=\"$snmysqlstoragepass\"" $mysqldbname >>$workingdir/error_logs/fog_error_${version}.log 2>&1
   99     errorStat $?
  100     dots "Granting access to fogstorage database user"
  101     mysql ${host} -s --user=fogstorage --password="${snmysqlstoragepass}" --execute="INSERT INTO $mysqldbname.taskLog VALUES ( 0, '999test', 3, '127.0.0.1', NOW(), 'fog');" >/dev/null 2>&1
  102     connect_as_fogstorage=$?
  103     if [[ $connect_as_fogstorage -eq 0 ]]; then
  104         mysql $sqloptionsuser --password="${snmysqlpass}" --execute="DELETE FROM $mysqldbname.taskLog WHERE taskID='999test' AND ip='127.0.0.1';" >/dev/null 2>&1
  105         echo "Skipped"
  106         return
  107     fi
  108 
  109     # we still need to grant access for the fogstorage DB user
  110     # and therefore need root DB access
  111     mysql $sqloptionsroot --password="${snmysqlrootpass}" --execute="quit" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  112     if [[ $? -ne 0 ]]; then
  113         echo
  114         echo "   To improve the overall security the installer will restrict"
  115         echo "   permissions for the *fogstorage* database user."
  116         echo "   Please provide the database *root* user password. Be asured"
  117         echo "   that this password will only be used while the FOG installer"
  118         echo -n "   is running and won't be stored anywhere: "
  119         read -rs snmysqlrootpass
  120         echo
  121         echo
  122         mysql $sqloptionsroot --password="${snmysqlrootpass}" --execute="quit" >/dev/null 2>&1
  123         if [[ $? -ne 0 ]]; then
  124             echo "   Unable to connect to the database using the given password!"
  125             echo -n "   Try again: "
  126             read -rs snmysqlrootpass
  127             mysql $sqloptionsroot --password="${snmysqlrootpass}" --execute="quit" >/dev/null 2>&1
  128             if [[ $? -ne 0 ]]; then
  129                 echo
  130                 echo "   Failed! Terminating installer now."
  131                 exit 1
  132             fi
  133         fi
  134     fi
  135     [[ ! -d ../tmp/ ]] && mkdir -p ../tmp/ >/dev/null 2>&1
  136     cat >../tmp/fog-db-grant-fogstorage-access.sql <<EOF
  137 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ANSI' ;
  138 GRANT SELECT ON $mysqldbname.* TO 'fogstorage'@'%' ;
  139 GRANT INSERT,UPDATE ON $mysqldbname.hosts TO 'fogstorage'@'%' ;
  140 GRANT INSERT,UPDATE ON $mysqldbname.inventory TO 'fogstorage'@'%' ;
  141 GRANT INSERT,UPDATE ON $mysqldbname.multicastSessions TO 'fogstorage'@'%' ;
  142 GRANT INSERT,UPDATE ON $mysqldbname.multicastSessionsAssoc TO 'fogstorage'@'%' ;
  143 GRANT INSERT,UPDATE ON $mysqldbname.nfsGroupMembers TO 'fogstorage'@'%' ;
  144 GRANT INSERT,UPDATE ON $mysqldbname.tasks TO 'fogstorage'@'%' ;
  145 GRANT INSERT,UPDATE ON $mysqldbname.taskStates TO 'fogstorage'@'%' ;
  146 GRANT INSERT,UPDATE ON $mysqldbname.taskLog TO 'fogstorage'@'%' ;
  147 GRANT INSERT,UPDATE ON $mysqldbname.snapinTasks TO 'fogstorage'@'%' ;
  148 GRANT INSERT,UPDATE ON $mysqldbname.snapinJobs TO 'fogstorage'@'%' ;
  149 GRANT INSERT,UPDATE ON $mysqldbname.imagingLog TO 'fogstorage'@'%' ;
  150 FLUSH PRIVILEGES ;
  151 SET SQL_MODE=@OLD_SQL_MODE ;
  152 EOF
  153     mysql $sqloptionsroot --password="${snmysqlrootpass}" <../tmp/fog-db-grant-fogstorage-access.sql >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  154     errorStat $?
  155 }
  156 validip() {
  157     local ip=$1
  158     local stat=1
  159     if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  160         OIFS=$IFS
  161         IFS='.'
  162         ip=($ip)
  163         IFS=$OIFS
  164         [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
  165         stat=$?
  166     fi
  167     echo $stat
  168 }
  169 getCidr() {
  170     local cidr
  171     cidr=$(ip -f inet -o addr | grep $1 | awk -F'[ /]+' '/global/ {print $5}' | head -n2 | tail -n1)
  172     echo $cidr
  173 }
  174 mask2cidr() {
  175     local submask=$1
  176     nbits=0
  177     OIFS=$IFS
  178     IFS='.'
  179     for dec in $submask; do
  180         case $dec in
  181             255)
  182                 let nbits+=8
  183                 ;;
  184             254)
  185                 let nbits+=7
  186                 break
  187                 ;;
  188             252)
  189                 let nbits+=6
  190                 break
  191                 ;;
  192             248)
  193                 let nbits+=5
  194                 break
  195                 ;;
  196             240)
  197                 let nbits+=4
  198                 break
  199                 ;;
  200             224)
  201                 let
  202                 nbits+=3
  203                 break
  204                 ;;
  205             192)
  206                 let nbits+=2
  207                 break
  208                 ;;
  209             128)
  210                 let nbits+=1
  211                 break
  212                 ;;
  213             0)
  214                 ;;
  215             *)
  216                 echo "Error: $dec is not recognized"
  217                 exit 1
  218                 ;;
  219         esac
  220     done
  221     IFS=$OIFS
  222     echo "$nbits"
  223 }
  224 cidr2mask() {
  225     local i=""
  226     local mask=""
  227     local full_octets=$(($1/8))
  228     local partial_octet=$(($1%8))
  229     for ((i=0;i<4;i+=1)); do
  230         if [[ $i -lt $full_octets ]]; then
  231             mask+=255
  232         elif [[ $i -eq $full_octets ]]; then
  233             mask+=$((256 - 2**(8-$partial_octet)))
  234         else
  235             mask+=0
  236         fi
  237         test $i -lt 3 && mask+=.
  238     done
  239     echo $mask
  240 }
  241 mask2network() {
  242     OIFS=$IFS
  243     IFS='.'
  244     read -r i1 i2 i3 i4 <<< "$1"
  245     read -r m1 m2 m3 m4 <<< "$2"
  246     IFS=$OIFS
  247     printf "%d.%d.%d.%d\n"  "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"
  248 }
  249 interface2broadcast() {
  250     local interface=$1
  251     if [[ -z $interface ]]; then
  252         echo "No interface passed"
  253         return 1
  254     fi
  255     echo $(ip -4 addr show | grep -w inet | grep $interface | awk '{print $4}')
  256 }
  257 subtract1fromAddress() {
  258     local ip=$1
  259     if [[ -z $ip ]]; then
  260         echo "No IP Passed"
  261         return 1
  262     fi
  263     if [[ ! $(validip $ip) -eq 0 ]]; then
  264         echo "Invalid IP Passed"
  265         return 1
  266     fi
  267     oIFS=$IFS
  268     IFS='.'
  269     read ip1 ip2 ip3 ip4 <<< "$ip"
  270     IFS=$oIFS
  271     if [[ $ip4 -gt 0 ]]; then
  272         let ip4-=1
  273     elif [[ $ip3 -gt 0 ]]; then
  274         let ip3-=1
  275         ip4=255
  276     elif [[ $ip2 -gt 0 ]]; then
  277         let ip2-=1
  278         ip3=255
  279         ip4=255
  280     elif [[ $ip1 -gt 0 ]]; then
  281         let ip1-=1
  282         ip2=255
  283         ip3=255
  284         ip4=255
  285     else
  286         echo "Invalid IP ranges were passed"
  287         echo ${ip1}.${ip2}.${ip3}.${ip4}
  288         return 2
  289     fi
  290     echo ${ip1}.${ip2}.${ip3}.${ip4}
  291 }
  292 subtractFromAddress() {
  293     local ipaddress="$1"
  294     local decreaseby=$2
  295     local maxOctetValue=256
  296     local octet1=""
  297     local octet2=""
  298     local octet3=""
  299     local octet4=""
  300     oIFS=$IFS
  301     IFS='.' read octet1 octet2 octet3 octet4 <<< "$ipaddress"
  302     IFS=$oIFS
  303     let octet4-=$decreaseby
  304     if [[ $octet4 -lt $maxOctetValue && $octet4 -ge 0 ]]; then
  305         printf "%d.%d.%d.%d\n" $octet1 $octet2 $octet3 $octet4 | sed 's/-//g'
  306         return 0
  307     fi
  308     echo $octet4
  309     echo $maxOctetValue
  310     octet4=$(echo $octet4 | sed 's/-//g')
  311     numRollOver=$((octet4 / maxOctetValue))
  312     echo $numRollOver
  313     let octet4-=$((numRollOver * maxOctetValue))
  314     echo $((numRollOver - octet3))
  315     let octet3-=$numRollOver
  316     echo $octet3
  317     if [[ $octet3 -lt $maxOctetValue && $octet3 -ge 0 ]]; then
  318         echo 'here'
  319         printf "%d.%d.%d.%d\n" $octet1 $octet2 $octet3 $octet4 | sed 's/-//g'
  320         return 0
  321     fi
  322     numRollOver=$((octet3 / maxOctetValue))
  323     let octet3-=$((numRollOver * maxOctetValue))
  324     let octet2-=$numRollOver
  325     if [[ $octet2 -lt $maxOctetValue && $octet2 -ge 0 ]]; then
  326         printf "%d.%d.%d.%d\n" $octet1 $octet2 $octet3 $octet4 | sed 's/-//g'
  327         return 0
  328     fi
  329     numRollOver=$((octet2 / maxOctetValue))
  330     let octet2-=$((numRollOver * maxOctetValue))
  331     let octet1-=$numRollOver
  332     if [[ $octet1 -lt $maxOctetValue && $octet1 -ge 0 ]]; then
  333         printf "%d.%d.%d.%d\n" $octet1 $octet2 $octet3 $octet4 | sed 's/-//g'
  334         return 0
  335     fi
  336     return 1
  337 }
  338 addToAddress() {
  339     local ipaddress="$1"
  340     local increaseby=$2
  341     local maxOctetValue=256
  342     local octet1=""
  343     local octet2=""
  344     local octet3=""
  345     local octet4=""
  346     oIFS=$IFS
  347     IFS='.' read octet1 octet2 octet3 octet4 <<< "$ipaddress"
  348     IFS=$oIFS
  349     let octet4+=$increaseby
  350     if [[ $octet4 -lt $maxOctetValue && $octet4 -ge 0 ]]; then
  351         printf "%d.%d.%d.%d\n" $octet1 $octet2 $octet3 $octet4
  352         return 0
  353     fi
  354     numRollOver=$((octet4 / maxOctetValue))
  355     let octet4-=$((numRollOver * maxOctetValue))
  356     let octet3+=$numRollOver
  357     if [[ $octet3 -lt $maxOctetValue && $octet3 -ge 0 ]]; then
  358         printf "%d.%d.%d.%d\n" $octet1 $octet2 $octet3 $octet4
  359         return 0
  360     fi
  361     numRollOver=$((octet3 / maxOctetValue))
  362     let octet3-=$((numRollOver * maxOctetValue))
  363     let octet2+=$numRollOver
  364     if [[ $octet2 -lt $maxOctetValue && $octet2 -ge 0 ]]; then
  365         printf "%d.%d.%d.%d\n" $octet1 $octet2 $octet3 $octet4
  366         return 0
  367     fi
  368     numRollOver=$((octet2 / maxOctetValue))
  369     let octet2-=$((numRollOver * maxOctetValue))
  370     let octet1+=$numRollOver
  371     if [[ $octet1 -lt $maxOctetValue && $octet1 -ge 0 ]]; then
  372         printf "%d.%d.%d.%d\n" $octet1 $octet2 $octet3 $octet4
  373         return 0
  374     fi
  375     return 1
  376 }
  377 getAllNetworkInterfaces() {
  378     gatewayif=$(ip -4 route show | grep "^default via" | awk '{print $5}')
  379     if [[ -z ${gatewayif} ]]; then
  380         interfaces="$(ip -4 link | grep -v LOOPBACK | grep UP | awk -F': |@' '{print $2}' | tr '\n' ' ')"
  381     else
  382         interfaces="$gatewayif $(ip -4 link | grep -v LOOPBACK | grep UP | awk -F': |@' '{print $2}' | tr '\n' ' ' | sed "s/${gatewayif}//g")"
  383     fi
  384     echo -n $interfaces
  385 }
  386 checkInternetConnection() {
  387     sites=( "k.root-servers.net" "m.root-servers.net" "f.root-servers.net" )
  388     ips=( "193.0.14.129" "202.12.27.33" "192.5.5.241" )
  389     dots "Testing internet connection"
  390     for i in $(seq 0 2); do
  391         ping -c 1 ${ips[$i]} >/dev/null 2>&1
  392         [[ $? -ne 0 ]] && continue
  393         ping -c 1 ${sites[$i]} >/dev/null 2>&1
  394         if [[ $? -ne 0 ]]; then
  395             echo "Internet connection detected but there seems to be a DNS problem." | tee -a $workingdir/error_logs/fog_error_${version}.log
  396             echo "Check the contents of /etc/resolv.conf" | tee -a $workingdir/error_logs/fog_error_${version}.log
  397             echo "If this is CentOS, RHEL, or Fedora or an other RH variant," | tee -a $workingdir/error_logs/fog_error_${version}.log
  398             echo "also check the DNS entries in /etc/sysconfig/network-scripts/ifcfg-*" | tee -a $workingdir/error_logs/fog_error_${version}.log
  399         fi
  400         echo "Done"
  401         return
  402     done
  403     echo "There was no interface with an active internet connection found." | tee -a $workingdir/error_logs/fog_error_${version}.log
  404     echo
  405 }
  406 join() {
  407     local IFS="$1"
  408     shift
  409     echo "$*"
  410 }
  411 restoreReports() {
  412     dots "Restoring user reports"
  413     if [[ -d $webdirdest/management/reports ]]; then
  414         if [[ -d ../rpttmp/ ]]; then
  415             cp -a ../rpttmp/* $webdirdest/management/reports/
  416         fi
  417     fi
  418     errorStat $?
  419 }
  420 installFOGServices() {
  421     dots "Setting up FOG Services"
  422     mkdir -p $servicedst
  423     cp -Rf $servicesrc/* $servicedst/
  424     chmod +x -R $servicedst/
  425     mkdir -p $servicelogs
  426     errorStat $?
  427 }
  428 configureUDPCast() {
  429     dots "Setting up UDPCast"
  430     cp -Rf "$udpcastsrc" "$udpcasttmp"
  431     cur=$(pwd)
  432     cd /tmp
  433     tar xvzf "$udpcasttmp" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  434     cd $udpcastout
  435     grep -q 'BCM[0-9][0-9][0-9][0-9]' /proc/cpuinfo >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  436     if [[ $? -eq 0 ]]; then
  437         wget -qO config.guess "https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  438         wget -qO config.sub "https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  439         chmod +x config.guess config.sub >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  440     fi
  441     errorStat $?
  442     dots "Configuring UDPCast"
  443     ./configure >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  444     errorStat $?
  445     dots "Building UDPCast"
  446     make >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  447     errorStat $?
  448     dots "Installing UDPCast"
  449     make install >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  450     errorStat $?
  451     cd $cur
  452 }
  453 configureFTP() {
  454     dots "Setting up and starting VSFTP Server..."
  455     if [[ -f $ftpxinetd ]]; then
  456         mv $ftpxinetd ${ftpxinetd}.fogbackup
  457     fi
  458     vsftp=$(vsftpd -version 0>&1 | awk -F'version ' '{print $2}')
  459     vsvermaj=$(echo $vsftp | awk -F. '{print $1}')
  460     vsverbug=$(echo $vsftp | awk -F. '{print $3}')
  461     seccompsand=""
  462     allow_writeable_chroot=""
  463     if [[ $vsvermaj -gt 3 ]] || [[ $vsvermaj -eq 3 && $vsverbug -ge 2 ]]; then
  464         seccompsand="seccomp_sandbox=NO"
  465     fi
  466     mv -fv "${ftpconfig}" "${ftpconfig}.${timestamp}" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  467     echo -e  "max_per_ip=200\nanonymous_enable=NO\nlocal_enable=YES\nwrite_enable=YES\nlocal_umask=022\ndirmessage_enable=YES\nxferlog_enable=YES\nconnect_from_port_20=YES\nxferlog_std_format=YES\nlisten=YES\npam_service_name=vsftpd\nuserlist_enable=NO\n$seccompsand" > "$ftpconfig"
  468     diffconfig "${ftpconfig}"
  469     case $systemctl in
  470         yes)
  471             systemctl enable vsftpd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  472             sleep 2
  473             systemctl stop vsftpd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  474             sleep 2
  475             systemctl start vsftpd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  476             sleep 2
  477             systemctl status vsftpd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  478             ;;
  479         *)
  480             case $osid in
  481                 2)
  482                     sysv-rc-conf vsftpd on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  483                     service vsftpd stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  484                     sleep 2
  485                     service vsftpd start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  486                     sleep 2
  487                     service vsftpd status >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  488                     ;;
  489                 *)
  490                     chkconfig vsftpd on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  491                     service vsftpd stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  492                     sleep 2
  493                     service vsftpd start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  494                     sleep 2
  495                     service vsftpd status >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  496                     ;;
  497             esac
  498             ;;
  499     esac
  500     errorStat $?
  501 }
  502 configureDefaultiPXEfile() {
  503     [[ -z $webroot ]] && webroot='/'
  504     echo -e "#!ipxe\ncpuid --ext 29 && set arch x86_64 || set arch \${buildarch}\nparams\nparam mac0 \${net0/mac}\nparam arch \${arch}\nparam platform \${platform}\nparam product \${product}\nparam manufacturer \${product}\nparam ipxever \${version}\nparam filename \${filename}\nparam sysuuid \${uuid}\nisset \${net1/mac} && param mac1 \${net1/mac} || goto bootme\nisset \${net2/mac} && param mac2 \${net2/mac} || goto bootme\n:bootme\nchain ${httpproto}://$ipaddress${webroot}service/ipxe/boot.php##params" > "$tftpdirdst/default.ipxe"
  505 }
  506 configureTFTPandPXE() {
  507     [[ -d ${tftpdirdst}.prev ]] && rm -rf ${tftpdirdst}.prev >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  508     [[ ! -d ${tftpdirdst} ]] && mkdir -p $tftpdirdst >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  509     [[ -e ${tftpdirdst}.fogbackup ]] && rm -rf ${tftpdirdst}.fogbackup >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  510     [[ -d $tftpdirdst && ! -d ${tftpdirdst}.prev ]] && mkdir -p ${tftpdirdst}.prev >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  511     [[ -d ${tftpdirdst}.prev ]] && cp -Rf $tftpdirdst/* ${tftpdirdst}.prev/ >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  512     if [[ "x$httpproto" = "xhttps" ]]; then
  513         dots "Compiling iPXE binaries trusting your SSL certificate"
  514         cd $buildipxesrc
  515         ./buildipxe.sh ${sslpath}CA/.fogCA.pem >>$workingdir/error_logs/fog_ipxe-build_${version}.log 2>&1
  516         errorStat $?
  517         cd $workingdir
  518     fi
  519     dots "Setting up and starting TFTP and PXE Servers"
  520     cd $tftpdirsrc
  521     find -type d -exec mkdir -p /tftpboot/{} \; >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  522     find -type f -exec cp -Rfv {} $tftpdirdst/{} \; >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  523     cd $workingdir
  524     chown -R $username $tftpdirdst >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  525     chown -R $username $webdirdest/service/ipxe >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  526     find $tftpdirdst -type d -exec chmod 755 {} \; >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  527     find $webdirdest -type d -exec chmod 755 {} \; >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  528     find $tftpdirdst ! -type d -exec chmod 655 {} \; >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  529     configureDefaultiPXEfile
  530     if [[ -f $tftpconfig ]]; then
  531         cp -Rf $tftpconfig ${tftpconfig}.fogbackup >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  532     fi
  533     if [[ $noTftpBuild != "true" ]]; then
  534         echo -e "# default: off\n# description: The tftp server serves files using the trivial file transfer \n#    protocol.  The tftp protocol is often used to boot diskless \n# workstations, download configuration files to network-aware printers, \n#   and to start the installation process for some operating systems.\nservice tftp\n{\n    socket_type     = dgram\n   protocol        = udp\n wait            = yes\n user            = root\n    server          = /usr/sbin/in.tftpd\n  server_args     = -s ${tftpdirdst}\n    disable         = no\n  per_source      = 11\n  cps         = 100 2\n   flags           = IPv4\n}" > "$tftpconfig"
  535     fi
  536     case $systemctl in
  537         yes)
  538             if [[ $osid -eq 2 && -f $tftpconfigupstartdefaults ]]; then
  539                 echo -e "# /etc/default/tftpd-hpa\n# FOG Modified version\nTFTP_USERNAME=\"root\"\nTFTP_DIRECTORY=\"/tftpboot\"\nTFTP_ADDRESS=\":69\"\nTFTP_OPTIONS=\"-s\"" > "$tftpconfigupstartdefaults"
  540                 systemctl disable xinetd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  541                 systemctl enable tftpd-hpa >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  542                 systemctl stop xinetd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  543                 sleep 2
  544                 systemctl stop tftpd-hpa >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  545                 sleep 2
  546                 systemctl start tftpd-hpa >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  547                 sleep 2
  548                 systemctl status tftpd-hpa >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  549             else
  550                 systemctl enable xinetd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  551                 systemctl stop xinetd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  552                 sleep 2
  553                 systemctl start xinetd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  554                 sleep 2
  555                 systemctl status xinetd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  556             fi
  557             ;;
  558         *)
  559             if [[ $osid -eq 2 && -f $tftpconfigupstartdefaults ]]; then
  560                 echo -e "# /etc/default/tftpd-hpa\n# FOG Modified version\nTFTP_USERNAME=\"root\"\nTFTP_DIRECTORY=\"/tftpboot\"\nTFTP_ADDRESS=\":69\"\nTFTP_OPTIONS=\"-s\"" > "$tftpconfigupstartdefaults"
  561                 sysv-rc-conf xinetd off >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  562                 service xinetd stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  563                 sysv-rc-conf tftpd-hpa on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  564                 service tftpd-hpa stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  565                 sleep 2
  566                 service tftpd-hpa start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  567                 sleep 2
  568             elif [[ $osid -eq 2 ]]; then
  569                 sysv-rc-conf xinetd on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  570                 $initdpath/xinetd stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  571                 sleep 2
  572                 $initdpath/xinetd start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  573                 sleep 2
  574             else
  575                 chkconfig xinetd on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  576                 service xinetd stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  577                 sleep 2
  578                 service xinetd start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  579                 sleep 2
  580                 service xinetd status >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  581             fi
  582             ;;
  583     esac
  584     errorStat $?
  585 }
  586 configureMinHttpd() {
  587     configureHttpd
  588     echo "<?php" > "$webdirdest/management/index.php"
  589     echo "/**" >> "$webdirdest/management/index.php"
  590     echo " * The main index presenter" >> "$webdirdest/management/index.php"
  591     echo " *" >> "$webdirdest/management/index.php"
  592     echo " * PHP version 5" >> "$webdirdest/management/index.php"
  593     echo " *" >> "$webdirdest/management/index.php"
  594     echo " * @category Index_Page" >> "$webdirdest/management/index.php"
  595     echo " * @package  FOGProject" >> "$webdirdest/management/index.php"
  596     echo " * @author   Tom Elliott <tommygunsster@gmail.com>" >> "$webdirdest/management/index.php"
  597     echo " * @license  http://opensource.org/licenses/gpl-3.0 GPLv3" >> "$webdirdest/management/index.php"
  598     echo " * @link     https://fogproject.org" >> "$webdirdest/management/index.php"
  599     echo " */" >> "$webdirdest/management/index.php"
  600     echo "/**" >> "$webdirdest/management/index.php"
  601     echo " * The main index presenter" >> "$webdirdest/management/index.php"
  602     echo " *" >> "$webdirdest/management/index.php"
  603     echo " * @category Index_Page" >> "$webdirdest/management/index.php"
  604     echo " * @package  FOGProject" >> "$webdirdest/management/index.php"
  605     echo " * @author   Tom Elliott <tommygunsster@gmail.com>" >> "$webdirdest/management/index.php"
  606     echo " * @license  http://opensource.org/licenses/gpl-3.0 GPLv3" >> "$webdirdest/management/index.php"
  607     echo " * @link     https://fogproject.org" >> "$webdirdest/management/index.php"
  608     echo " */" >> "$webdirdest/management/index.php"
  609     echo "require '../commons/base.inc.php';" >> "$webdirdest/management/index.php"
  610     echo "require '../commons/text.php';" >> "$webdirdest/management/index.php"
  611     echo "ob_start();" >> "$webdirdest/management/index.php"
  612     echo "FOGCore::getClass('FOGPageManager')->render();" >> "$webdirdest/management/index.php"
  613     echo "ob_end_clean();" >> "$webdirdest/management/index.php"
  614     echo "die(_('This is a storage node, please do not access the web ui here!'));" >> "$webdirdest/management/index.php"
  615 }
  616 addOndrejRepo() {
  617     find /etc/apt/sources.list.d/ -name '*ondrej*' -exec rm -rf {} \; >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  618     DEBIAN_FRONTEND=noninteractive $packageinstaller python-software-properties >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  619     DEBIAN_FRONTEND=noninteractive $packageinstaller software-properties-common >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  620     DEBIAN_FRONTEND=noninteractive $packageinstaller ntpdate >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  621     ntpdate pool.ntp.org >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  622     locale-gen 'en_US.UTF-8' >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  623     LANG='en_US.UTF-8' LC_ALL='en_US.UTF-8' add-apt-repository -y ppa:ondrej/${repo} >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  624     LANG='en_US.UTF-8' LC_ALL='en_US.UTF-8' add-apt-repository -y ppa:ondrej/apache2 >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  625 }
  626 installPackages() {
  627     [[ $installlang -eq 1 ]] && packages="$packages gettext"
  628     packages="$packages unzip"
  629     dots "Adjusting repository (can take a long time for cleanup)"
  630     case $osid in
  631         1)
  632             packages="$packages php-bcmath bc"
  633             packages="${packages// mod_fastcgi/}"
  634             packages="${packages// mod_evasive/}"
  635             packages="${packages// php-mcrypt/}"
  636             case $linuxReleaseName in
  637                 *[Ff][Ee][Dd][Oo][Rr][Aa]*)
  638                     packages="$packages php-json"
  639                     packages="${packages// mysql / mariadb }">>$workingdir/error_logs/fog_error_${version}.log 2>&1
  640                     packages="${packages// mysql-server / mariadb-server }">>$workingdir/error_logs/fog_error_${version}.log 2>&1
  641                     packages="${packages// dhcp / dhcp-server }">>$workingdir/error_logs/fog_error_${version}.log 2>&1
  642                     ;;
  643                 *)
  644                     x="epel-release"
  645                     eval $packageQuery >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  646                     if [[ ! $? -eq 0 ]]; then
  647                         y="https://dl.fedoraproject.org/pub/epel/epel-release-latest-${OSVersion}.noarch.rpm"
  648                         $packageinstaller $y >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  649                         errorStat $? "skipOk"
  650                     fi
  651                     y="http://rpms.remirepo.net/enterprise/remi-release-${OSVersion}.rpm"
  652                     x="$(basename $y | awk -F[.] '{print $1}')*"
  653                     eval $packageQuery >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  654                     if [[ ! $? -eq 0 ]]; then
  655                         rpm -Uvh $y >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  656                         errorStat $? "skipOk"
  657                     fi
  658                     rpm --import "http://rpms.remirepo.net/RPM-GPG-KEY-remi" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  659                     errorStat $? "skipOk"
  660                     if [[ -n $repoenable ]]; then
  661                         if [[ $OSVersion -le 7 ]]; then
  662                             $repoenable epel >>$workingdir/error_logs/fog_error_${version}.log 2>&1 || true
  663                             $repoenable remi >>$workingdir/error_logs/fog_error_${version}.log 2>&1 || true
  664                             $repoenable remi-php72 >>$workingdir/error_logs/fog_error_${version}.log 2>&1 || true
  665                         fi
  666                     fi
  667                     ;;
  668             esac
  669             ;;
  670         2)
  671             packages="${packages// libapache2-mod-fastcgi/}"
  672             packages="${packages// libapache2-mod-evasive/}"
  673             packages="${packages// php${php_ver}-mcrypt/}"
  674             packages="${packages} php${php_ver}-bcmath bc"
  675             case $linuxReleaseName in
  676                 *[Uu][Bb][Uu][Nn][Tt][Uu]*|*[Mm][Ii][Nn][Tt]*)
  677                     if [[ $OSVersion -gt 17 ]]; then
  678                         packages="${packages// libcurl3 / libcurl4 }">>$workingdir/error_logs/fog_error_${version}.log 2>&1
  679                     fi
  680                     if [[ $linuxReleaseName == +(*[Uu][Bb][Uu][Nn][Tt][Uu]*) && $OSVersion -ge 18 ]]; then
  681                         # Fix missing universe section for Ubuntu 18.04 LIVE
  682                         LANG='en_US.UTF-8' LC_ALL='en_US.UTF-8' add-apt-repository -y universe >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  683                         # check to see if we still have packages from deb.sury.org (a.k.a ondrej) installed and try to clean it up
  684                         dpkg -l | grep -q "deb\.sury\.org"
  685                         if [[ $? -eq 0 ]]; then
  686                             # make sure we have ondrej repos enabled to be able to use ppa-purge
  687                             addOndrejRepo
  688                             # use ppa-purge to not just remove the repo but also downgrade packages to Ubuntu original versions
  689                             DEBIAN_FRONTEND=noninteractive apt-get install -yq ppa-purge >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  690                             ppa-purge -y ppa:ondrej/apache2 >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  691                             # for php we want to purge all packages first as we don't want ppa-purge to try downgrading those
  692                             DEBIAN_FRONTEND=noninteractive apt-get purge -yq 'php5*' 'php7*' 'libapache*' >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  693                             ppa-purge -y ppa:ondrej/php >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  694                             DEBIAN_FRONTEND=noninteractive apt-get purge -yq ppa-purge >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  695                         fi
  696                     else
  697                         addOndrejRepo
  698                     fi
  699                     ;;
  700                 *[Bb][Ii][Aa][Nn]*)
  701                     if [[ $OSVersion -ge 10 ]]; then
  702                         packages="${packages// libcurl3 / libcurl4 }">>$workingdir/error_logs/fog_error_${version}.log 2>&1
  703                         packages="${packages// mysql-client / mariadb-client }">>$workingdir/error_logs/fog_error_${version}.log 2>&1
  704                         packages="${packages// mysql-server / mariadb-server }">>$workingdir/error_logs/fog_error_${version}.log 2>&1
  705                     fi
  706                     ;;
  707 
  708             esac
  709             ;;
  710         3)
  711             echo $packages | grep -q -v " git" && packages="${packages} git"
  712             packages="${packages// php-mcrypt/}"
  713             ;;
  714     esac
  715     errorStat $?
  716     dots "Preparing Package Manager"
  717     $packmanUpdate >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  718     if [[ $osid -eq 2 ]]; then
  719         if [[ $? != 0 ]] && [[ $linuxReleaseName == +(*[Uu][Bb][Uu][Nn][Tt][Uu]*|*[Mm][Ii][Nn][Tt]*) ]]; then
  720             cp /etc/apt/sources.list /etc/apt/sources.list.original_fog_$(date +%s)
  721             sed -i -e 's/\/\/*archive.ubuntu.com\|\/\/*security.ubuntu.com/\/\/old-releases.ubuntu.com/g' /etc/apt/sources.list
  722             $packmanUpdate >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  723             if [[ $? != 0 ]]; then
  724                 cp -f /etc/apt/sources.list.original_fog /etc/apt/sources.list >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  725                 rm -f /etc/apt/sources.list.original_fog >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  726                 false
  727             fi
  728         fi
  729     fi
  730     errorStat $?
  731     packages=$(echo ${packages[@]} | tr ' ' '\n' | sort -u | tr '\n' ' ')
  732     echo -e " * Packages to be installed:\n\n\t$packages\n\n"
  733     newPackList=""
  734     local toInstall=""
  735     for x in $packages; do
  736         case $x in
  737             mysql)
  738                 for sqlclient in $sqlclientlist; do
  739                     eval $packagelist "$sqlclient" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  740                     if [[ $? -eq 0 ]]; then
  741                         x=$sqlclient
  742                         break
  743                     fi
  744                 done
  745                 ;;
  746             mysql-server)
  747                 for sqlserver in $sqlserverlist; do
  748                     eval $packagelist "$sqlserver" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  749                     if [[ $? -eq 0 ]]; then
  750                         x=$sqlserver
  751                         break
  752                     fi
  753                 done
  754                 ;;
  755             php${php_ver}-json)
  756                 for json in $jsontest; do
  757                     eval $packagelist "$json" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  758                     if [[ $? -eq 0 ]]; then
  759                         x=$json
  760                         break
  761                     fi
  762                 done
  763                 ;;
  764             php${php_ver}-mysql*)
  765                 for phpmysql in $(echo php${php_ver}-mysqlnd php${php_ver}-mysql); do
  766                     eval $packagelist "$phpmysql" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  767                     if [[ $? -eq 0 ]]; then
  768                         x=$phpmysql
  769                         break
  770                     fi
  771                 done
  772                 ;;
  773         esac
  774         [[ $osid == 2 && -z $dhcpd && $x == +(*'dhcp'*) ]] && dhcpd=$x
  775         eval $packageQuery >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  776         if [[ $? -eq 0 ]]; then
  777             dots "Skipping package:   $x"
  778             echo "(Already Installed)"
  779             newPackList="$newPackList $x"
  780             continue
  781         fi
  782         eval $packagelist "$x" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  783         if [[ ! $? -eq 0 ]]; then
  784             dots "Skipping package: $x"
  785             echo "(Does not exist)"
  786             continue
  787         fi
  788         newPackList="$newPackList $x"
  789         dots "Installing package: $x"
  790         DEBIAN_FRONTEND=noninteractive $packageinstaller $x >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  791         if [[ ! $? -eq 0 ]]; then
  792             echo "Failed! (Will try later)"
  793             [[ -z $toInstall ]] && toInstall="$x" || toInstall="$toInstall $x"
  794         else
  795             echo "OK"
  796         fi
  797     done
  798     packages=$newPackList
  799     packages=$(echo ${packages[@]} | tr ' ' '\n' | sort -u | tr '\n' ' ')
  800     dots "Updating packages as needed"
  801     DEBIAN_FRONTEND=noninteractive $packageupdater $packages >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  802     echo "OK"
  803     if [[ -n $toInstall ]]; then
  804         toInstall=$(echo ${toInstall[@]} | tr ' ' '\n' | sort -u | tr '\n' ' ')
  805         dots "Installing now everything is updated"
  806         DEBIAN_FRONTEND=noninteractive $packageinstaller $toInstall >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  807         errorStat $?
  808     fi
  809 }
  810 confirmPackageInstallation() {
  811     for x in $packages; do
  812         dots "Checking package: $x"
  813         eval $packageQuery >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  814         errorStat $?
  815     done
  816 }
  817 checkSELinux() {
  818     command -v sestatus >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  819     exitcode=$?
  820     [[ $exitcode -ne 0 ]] && return
  821     currentmode=$(LANG=C sestatus | grep "^Current mode" | awk '{print $3}')
  822     configmode=$(LANG=C sestatus | grep "^Mode from config file" | awk '{print $5}')
  823     [[ "$currentmode" != "enforcing" && "$configmode" != "enforcing" ]] && return
  824     echo " * SELinux is currently enabled on your system. This is often causing"
  825     echo " * issues and we recommend to disable it on FOG servers as of now."
  826     echo -n " * Should the installer disable SELinux for you now? (Y/n) "
  827     sedisable=""
  828     while [[ -z $sedisable ]]; do
  829         if [[ -n $autoaccept ]]; then
  830             sedisable="Y"
  831         else
  832             read -r sedisable
  833         fi
  834         case $sedisable in
  835             [Yy]|[Yy][Ee][Ss]|"")
  836                 sedisable="Y"
  837                 setenforce 0
  838                 sed -i 's/^SELINUX=.*$/SELINUX=permissive/' /etc/selinux/config
  839                 echo -e " * SELinux disabled - proceeding with installation...\n"
  840                 ;;
  841             [Nn]|[Nn][Oo])
  842                 echo " * You sure know what you are doing, just keep in mind we told you! :-)"
  843                 ;;
  844             *)
  845                 sedisable=""
  846                 echo " * Invalid input, please try again!"
  847                 ;;
  848         esac
  849     done
  850 }
  851 checkFirewall() {
  852     command -v iptables >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  853     iptcmd=$?
  854     if [[ $iptcmd -eq 0 ]]; then
  855         rulesnum=$(iptables -L -n | wc -l)
  856         policy=$(iptables -L -n | grep "^Chain" | grep -v "ACCEPT" -c)
  857         [[ $rulesnum -ne 8 || $policy -ne 0 ]] && fwrunning=1
  858     fi
  859     command -v firewall-cmd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
  860     fwcmd=$?
  861     if [[ $fwcmd -eq 0 ]]; then
  862         fwstate=$(firewall-cmd --state 2>&1)
  863         [[ "x$fwstate" == "xrunning" ]] && fwrunning=1
  864     fi
  865     [[ $fwrunning -ne 1 ]] && return
  866     echo " * The local firewall seems to be currently enabled on your system. This can cause"
  867     echo " * issues on FOG servers if you are not well experienced and know what you are doing."
  868     echo -n " * Should the installer try to disable the local firewall for you now? (y/N) "
  869     fwdisable=""
  870     while [[ -z $fwdisable ]]; do
  871         if [[ -n $autoaccept ]]; then
  872             fwdisable="N"
  873         else
  874             read -r fwdisable
  875         fi
  876         case $fwdisable in
  877             [Yy]|[Yy][Ee][Ss])
  878                 ufw stop >/dev/null 2>&1
  879                 ufw disable >/dev/null 2>&1
  880                 systemctl stop ufw >/dev/null 2>&1
  881                 systemctl disable ufw >/dev/null 2>&1
  882                 systemctl stop firewalld >/dev/null 2>&1
  883                 systemctl disable firewalld >/dev/null 2>&1
  884                 systemctl stop iptables >/dev/null 2>&1
  885                 systemctl disable iptables >/dev/null 2>&1
  886                 if [[ $iptcmd -eq 0 ]]; then
  887                     rulesnum=$(iptables -L -n | wc -l)
  888                     policy=$(iptables -L -n | grep "^Chain" | grep -v "ACCEPT" -c)
  889                     [[ $rulesnum -ne 8 || $policy -ne 0 ]] && cannotdisablefw=1
  890                 fi
  891                 if [[ $fwcmd -eq 0 ]]; then
  892                     fwstate=$(firewall-cmd --state 2>&1)
  893                     [[ "x$fwstate" == "xrunning" ]] && cannotdisablefw=1
  894                 fi
  895                 if [[ $cannotdisablefw -eq 1 ]]; then
  896                     echo " * We were unable to disable the firewall on your system. Read up on how"
  897                     echo " * you can disable it manually. Proceeding with the installation anyway..."
  898                     echo " * Hit [Enter] so we know you've read this message."
  899                     read
  900                 else
  901                     echo -e " * Firewall disabled - proceeding with installation...\n"
  902                 fi
  903                 ;;
  904             [Nn]|[Nn][Oo]|"")
  905                 fwdisable="N"
  906                 echo " * You sure know what you are doing, just keep in mind we told you! :-)"
  907                 if [[ -z $autoaccept ]]; then
  908                     echo " * Hit ENTER so we know you've read this message."
  909                     read
  910                 fi
  911                 ;;
  912             *)
  913                 fwdisable=""
  914                 echo " * Invalid input, please try again!"
  915                 ;;
  916         esac
  917     done
  918 }
  919 displayOSChoices() {
  920     blFirst=1
  921     while [[ -z $osid ]]; do
  922         if [[ $fogupdateloaded -eq 1 && $blFirst -eq 1 ]]; then
  923             blFirst=0
  924         else
  925             osid=$strSuggestedOS
  926             if [[ -z $autoaccept && ! -z $osid ]]; then
  927                 echo "  What version of Linux would you like to run the installation for?"
  928                 echo
  929                 echo "          1) Redhat Based Linux (Redhat, CentOS, Mageia)"
  930                 echo "          2) Debian Based Linux (Debian, Ubuntu, Kubuntu, Edubuntu)"
  931                 echo "          3) Arch Linux"
  932                 echo
  933                 echo -n "  Choice: [$strSuggestedOS] "
  934                 read osid
  935                 case $osid in
  936                     "")
  937                         osid=$strSuggestedOS
  938                         break
  939                         ;;
  940                     1|2|3)
  941                         break
  942                         ;;
  943                     *)
  944                         echo "  Invalid input, please try again."
  945                         osid=""
  946                         ;;
  947                 esac
  948             fi
  949         fi
  950     done
  951     doOSSpecificIncludes
  952 }
  953 doOSSpecificIncludes() {
  954     echo
  955     case $osid in
  956         1)
  957             echo -e "\n\n  Starting Redhat based Installation\n\n"
  958             osname="Redhat"
  959             . ../lib/redhat/config.sh
  960             ;;
  961         2)
  962             echo -e "\n\n  Starting Debian based Installation\n\n"
  963             osname="Debian"
  964             . ../lib/ubuntu/config.sh
  965             ;;
  966         3)
  967             echo -e "\n\n  Starting Arch Installation\n\n"
  968             osname="Arch"
  969             . ../lib/arch/config.sh
  970             systemctl="yes"
  971             ;;
  972         *)
  973             echo -e "  Sorry, answer not recognized\n\n"
  974             sleep 2
  975             osid=""
  976             ;;
  977     esac
  978     currentdir=$(pwd)
  979     case $currentdir in
  980         *$webdirdest*|*$tftpdirdst*)
  981             echo "Please change installation directory."
  982             echo "Running from here will fail."
  983             echo "You are in $currentdir which is a folder that will"
  984             echo "be moved during installation."
  985             exit 1
  986             ;;
  987     esac
  988 }
  989 errorStat() {
  990     local status=$1
  991     local skipOk=$2
  992     if [[ $status != 0 ]]; then
  993         echo "Failed!"
  994         if [[ -z $exitFail ]]; then
  995             echo
  996             echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  997             echo "!! The installer was not able to run all the way to the end as   !!"
  998             echo "!! something has caused it to fail. The following few lines are  !!"
  999             echo "!! from the error log file which might help us figure out what's !!"
 1000             echo "!! wrong. Please add this information when reporting an error.   !!"
 1001             echo "!! As well you might want to take a look at the full error log   !!"
 1002             echo "!! in $workingdir/error_logs/fog_error_${version}.log !!"
 1003             echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
 1004             echo
 1005             tail -n 5 $workingdir/error_logs/fog_error_${version}.log
 1006             exit $status
 1007         fi
 1008     fi
 1009     [[ -z $skipOk ]] && echo "OK"
 1010 }
 1011 stopInitScript() {
 1012     for serviceItem in $serviceList; do
 1013         dots "Stopping $serviceItem Service"
 1014         if [ "$systemctl" == "yes" ]; then
 1015             systemctl is-active --quiet $serviceItem && systemctl stop $serviceItem >>$workingdir/error_logs/fog_error_${version}.log 2>&1 && sleep 2
 1016         else
 1017             [[ ! -x $initdpath/$serviceItem ]] && continue
 1018             $initdpath/$serviceItem status >/dev/null 2>&1 && $initdpath/$serviceItem stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1 && sleep 2
 1019         fi
 1020         echo "OK"
 1021     done
 1022 }
 1023 startInitScript() {
 1024     for serviceItem in $serviceList; do
 1025         dots "Starting $serviceItem Service"
 1026         if [[ $systemctl == yes ]]; then
 1027             systemctl is-active --quiet $serviceItem || systemctl start $serviceItem >>$workingdir/error_logs/fog_error_${version}.log 2>&1 && sleep 2
 1028         else
 1029             [[ ! -x $initdpath/$serviceItem ]] && continue
 1030             $initdpath/$serviceItem status >/dev/null 2>&1 || $initdpath/$serviceItem start >>$workingdir/error_logs/fog_error_${version}.log 2>&1 && sleep 2
 1031         fi
 1032         errorStat $?
 1033     done
 1034 }
 1035 enableInitScript() {
 1036     for serviceItem in $serviceList; do
 1037         case $systemctl in
 1038             yes)
 1039                 dots "Setting permissions on $serviceItem script"
 1040                 chmod 644 $initdpath/$serviceItem >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1041                 errorStat $?
 1042                 dots "Enabling $serviceItem Service"
 1043                 systemctl enable $serviceItem >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1044                 if [[ ! $? -eq 0 && $osid -eq 2 ]]; then
 1045                     update-rc.d $(echo $serviceItem | sed -e 's/[.]service//g') enable 2 >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1046                     update-rc.d $(echo $serviceItem | sed -e 's/[.]service//g') enable 3 >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1047                     update-rc.d $(echo $serviceItem | sed -e 's/[.]service//g') enable 4 >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1048                     update-rc.d $(echo $serviceItem | sed -e 's/[.]service//g') enable 5 >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1049                 fi
 1050                 ;;
 1051             *)
 1052                 dots "Setting $serviceItem script executable"
 1053                 chmod +x $initdpath/$serviceItem >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1054                 errorStat $?
 1055                 case $osid in
 1056                     1)
 1057                         dots "Enabling $serviceItem Service"
 1058                         chkconfig $serviceItem on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1059                         ;;
 1060                     2)
 1061                         dots "Enabling $serviceItem Service"
 1062                         sysv-rc-conf $serviceItem off >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1063                         sysv-rc-conf $serviceItem on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1064                         case $linuxReleaseName in
 1065                             *[Uu][Bb][Uu][Nn][Tt][Uu]*|*[Mm][Ii][Nn][Tt]*)
 1066                                 /usr/lib/insserv/insserv -r $initdpath/$serviceItem >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1067                                 /usr/lib/insserv/insserv -d $initdpath/$serviceItem >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1068                                 ;;
 1069                             *)
 1070                                 insserv -r $initdpath/$serviceItem >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1071                                 insserv -d $initdpath/$serviceItem >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1072                                 ;;
 1073                         esac
 1074                         ;;
 1075                 esac
 1076                 ;;
 1077         esac
 1078         errorStat $?
 1079     done
 1080 }
 1081 installInitScript() {
 1082     dots "Installing FOG System Scripts"
 1083     cp -f $initdsrc/* $initdpath/ >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1084     errorStat $?
 1085     echo
 1086     echo
 1087     echo " * Configuring FOG System Services"
 1088     echo
 1089     echo
 1090     enableInitScript
 1091 }
 1092 configureMySql() {
 1093     stopInitScript
 1094     dots "Setting up and starting MySQL"
 1095     dbservice=$(systemctl list-units | grep -o -e "mariadb\.service" -e "mysqld\.service" -e "mysql\.service" | tr -d '@')
 1096     [[ -z $dbservice ]] && dbservice=$(systemctl list-unit-files | grep -v bad | grep -o -e "mariadb\.service" -e "mysqld\.service" -e "mysql\.service" | tr -d '@')
 1097     for mysqlconf in $(grep -rl '.*skip-networking' /etc | grep -v init.d); do
 1098         sed -i '/.*skip-networking/ s/^#*/#/' -i $mysqlconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1099     done
 1100     for mysqlconf in `grep -rl '.*bind-address.*=.*127.0.0.1' /etc | grep -v init.d`; do
 1101         sed -e '/.*bind-address.*=.*127.0.0.1/ s/^#*/#/' -i $mysqlconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1102     done
 1103     if [[ $systemctl == yes ]]; then
 1104         if [[ $osid -eq 3 && ! -f /var/lib/mysql/ibdata1 ]]; then
 1105             mkdir -p /var/lib/mysql >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1106             chown -R mysql:mysql /var/lib/mysql >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1107             mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1108         fi
 1109         systemctl is-enabled --quiet $dbservice || systemctl enable $dbservice >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1110         systemctl is-active --quiet $dbservice && systemctl stop $dbservice >>$workingdir/error_logs/fog_error_${version}.log 2>&1 && sleep 2
 1111         systemctl start $dbservice >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1112     else
 1113         case $osid in
 1114             1)
 1115                 chkconfig mysqld on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1116                 service mysqld start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1117                 ;;
 1118             2)
 1119                 sysv-rc-conf mysql on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1120                 service mysql start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1121                 ;;
 1122         esac
 1123     fi
 1124     # if someone still has DB user root set in .fogsettings we want to change that
 1125     [[ "x$snmysqluser" == "xroot" ]] && snmysqluser='fogmaster'
 1126     [[ -z $snmysqlpass ]] && snmysqlpass=$(generatePassword 20)
 1127     [[ -n $snmysqlhost ]] && host="--host=$snmysqlhost"
 1128     sqloptionsroot="${host} --user=root"
 1129     sqloptionsuser="${host} -s --user=${snmysqluser}"
 1130     mysqladmin $host ping >/dev/null 2>&1 || mysqladmin $host ping >/dev/null 2>&1 || mysqladmin $host ping >/dev/null 2>&1
 1131     errorStat $?
 1132 
 1133     dots "Setting up MySQL user and database"
 1134     mysql $sqloptionsroot --execute="quit" >/dev/null 2>&1
 1135     connect_as_root=$?
 1136     if [[ $connect_as_root -eq 0 ]]; then
 1137         mysqlrootauth=$(mysql $sqloptionsroot --database=mysql --execute="SELECT Host,User,plugin FROM user WHERE Host='localhost' AND User='root' AND plugin='unix_socket'")
 1138         if [[ -z $mysqlrootauth && -z $autoaccept ]]; then
 1139             echo
 1140             echo "   The installer detected a blank database *root* password. This"
 1141             echo "   is very common on a new install or if you upgrade from any"
 1142             echo "   version of FOG before 1.5.8. To improve overall security we ask"
 1143             echo "   you to supply an appropriate database *root* password now."
 1144             echo
 1145             echo "   NOTICE: Make sure you choose a good password but also one"
 1146             echo "   you can remember or use a password manager to store it."
 1147             echo "   The installer won't store the given password in any place"
 1148             echo "   and it will be lost right after the installer finishes!"
 1149             echo
 1150             echo -n "   Please enter a new database *root* password to be set: "
 1151             read -rs snmysqlrootpass
 1152             echo
 1153             echo
 1154             if [[ -z $snmysqlrootpass ]]; then
 1155                 snmysqlrootpass=$(generatePassword 20)
 1156                 echo
 1157                 echo "   We don't accept a blank database *root* password anymore and"
 1158                 echo "   will generate a password for you to use. Please make sure"
 1159                 echo "   you save the following password in an appropriate place as"
 1160                 echo "   the installer won't store it for you."
 1161                 echo
 1162                 echo "   Database root password: $snmysqlrootpass"
 1163                 echo
 1164                 echo "   Press [Enter] to procede..."
 1165                 read -rs procede
 1166                 echo
 1167                 echo
 1168             fi
 1169             # WARN: Since MariaDB 10.3 (maybe earlier) setting a password when auth plugin is
 1170             # set to unix_socket will actually switch to auth plugin mysql_native_password
 1171             # automatically which was not the case in MariaDB 10.1 and is causing trouble.
 1172             # So now we try to be more conservative and only reset the pass when we get one
 1173             # to make sure the user is in charge of this.
 1174             mysqladmin $sqloptionsroot password "${snmysqlrootpass}" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1175         fi
 1176         snmysqlstoragepass=$(mysql -s $sqloptionsroot --password="${snmysqlrootpass}" --execute="SELECT settingValue FROM globalSettings WHERE settingKey LIKE '%FOG_STORAGENODE_MYSQLPASS%'" $mysqldbname 2>/dev/null | tail -1)
 1177     else
 1178         snmysqlstoragepass=$(mysql $sqloptionsuser --password="${snmysqlpass}" --execute="SELECT settingValue FROM globalSettings WHERE settingKey LIKE '%FOG_STORAGENODE_MYSQLPASS%'" $mysqldbname 2>/dev/null | tail -1)
 1179     fi
 1180     mysql $sqloptionsuser --password="${snmysqlpass}" --execute="quit" >/dev/null 2>&1
 1181     connect_as_fogmaster=$?
 1182     mysql ${host} -s --user=fogstorage --password="${snmysqlstoragepass}" --execute="quit" >/dev/null 2>&1
 1183     connect_as_fogstorage=$?
 1184     if [[ $connect_as_fogmaster -eq 0 && $connect_as_fogstorage -eq 0 ]]; then
 1185         echo "Skipped"
 1186         return
 1187     fi
 1188 
 1189     # If we reach this point it's clear that this install is not setup with
 1190     # unpriviledged DB users yet and we need to have root DB access now.
 1191     if [[ $connect_as_root -ne 0 ]]; then
 1192         echo
 1193         echo "   To improve the overall security the installer will create an"
 1194         echo "   unpriviledged database user account for FOG's database access."
 1195         echo "   Please provide the database *root* user password. Be asured"
 1196         echo "   that this password will only be used while the FOG installer"
 1197         echo -n "   is running and won't be stored anywhere: "
 1198         read -rs snmysqlrootpass
 1199         echo
 1200         echo
 1201         mysql $sqloptionsroot --password="${snmysqlrootpass}" --execute="quit" >/dev/null 2>&1
 1202         if [[ $? -ne 0 ]]; then
 1203             echo "   Unable to connect to the database using the given password!"
 1204             echo -n "   Try again: "
 1205             read -rs snmysqlrootpass
 1206             mysql $sqloptionsroot --password="${snmysqlrootpass}" --execute="quit" >/dev/null 2>&1
 1207             if [[ $? -ne 0 ]]; then
 1208                 echo
 1209                 echo "   Failed! Terminating installer now."
 1210                 exit 1
 1211             fi
 1212         fi
 1213     fi
 1214 
 1215     snmysqlstoragepass=$(mysql -s $sqloptionsroot --password="${snmysqlrootpass}" --execute="SELECT settingValue FROM globalSettings WHERE settingKey LIKE '%FOG_STORAGENODE_MYSQLPASS%'" $mysqldbname 2>/dev/null | tail -1)
 1216     # generate a new fogstorage password if it doesn't exist yet or if it's old style fs0123456789
 1217     if [[ -z $snmysqlstoragepass ]]; then
 1218         snmysqlstoragepass=$(generatePassword 20)
 1219     elif [[ -n $(echo $snmysqlstoragepass | grep "^fs[0-9][0-9]*$") ]]; then
 1220         snmysqlstoragepass=$(generatePassword 20)
 1221         echo
 1222         echo "   The current *fogstorage* database password does not meet high"
 1223         echo "   security standards. We will generate a new password and update"
 1224         echo "   all the settings on this FOG server for you. Please take note"
 1225         echo "   of the following credentials that you need to manually update"
 1226         echo "   on all your storage nodes' /opt/fog/.fogsettings configuration"
 1227         echo "   files and re-run (!) the FOG installer:"
 1228         echo "   snmysqluser='fogstorage'"
 1229         echo "   snmysqlpass='${snmysqlstoragepass}'"
 1230         echo
 1231         if [[ -z $autoaccept ]]; then
 1232             echo "   Press [Enter] to proceed after you noted down the credentials."
 1233             read
 1234         fi
 1235     fi
 1236     [[ ! -d ../tmp/ ]] && mkdir -p ../tmp/ >/dev/null 2>&1
 1237     cat >../tmp/fog-db-and-user-setup.sql <<EOF
 1238 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ANSI' ;
 1239 DELETE FROM mysql.user WHERE User='' ;
 1240 DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1') ;
 1241 DROP DATABASE IF EXISTS test ;
 1242 DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%' ;
 1243 CREATE DATABASE IF NOT EXISTS $mysqldbname ;
 1244 USE $mysqldbname ;
 1245 DROP PROCEDURE IF EXISTS $mysqldbname.create_user_if_not_exists ;
 1246 DELIMITER $$
 1247 CREATE PROCEDURE $mysqldbname.create_user_if_not_exists()
 1248 BEGIN
 1249   DECLARE masteruser BIGINT DEFAULT 0 ;
 1250   DECLARE storageuser BIGINT DEFAULT 0 ;
 1251 
 1252   SELECT COUNT(*) INTO masteruser FROM mysql.user
 1253     WHERE User = '${snmysqluser}' and  Host = '${snmysqlhost}' ;
 1254   IF masteruser > 0 THEN
 1255     DROP USER '${snmysqluser}'@'${snmysqlhost}';
 1256   END IF ;
 1257   CREATE USER '${snmysqluser}'@'${snmysqlhost}' IDENTIFIED BY '${snmysqlpass}' ;
 1258   GRANT ALL PRIVILEGES ON $mysqldbname.* TO '${snmysqluser}'@'${snmysqlhost}' ;
 1259 
 1260   SELECT COUNT(*) INTO storageuser FROM mysql.user
 1261     WHERE User = 'fogstorage' and  Host = '%' ;
 1262   IF storageuser > 0 THEN
 1263     DROP USER 'fogstorage'@'%';
 1264   END IF ;
 1265   CREATE USER 'fogstorage'@'%' IDENTIFIED BY '${snmysqlstoragepass}' ;
 1266 END ;$$
 1267 DELIMITER ;
 1268 CALL $mysqldbname.create_user_if_not_exists() ;
 1269 DROP PROCEDURE IF EXISTS $mysqldbname.create_user_if_not_exists ;
 1270 FLUSH PRIVILEGES ;
 1271 SET SQL_MODE=@OLD_SQL_MODE ;
 1272 EOF
 1273     mysql $sqloptionsroot --password="${snmysqlrootpass}" <../tmp/fog-db-and-user-setup.sql >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1274     errorStat $?
 1275 }
 1276 configureFOGService() {
 1277     [[ ! -d $servicedst ]] && mkdir -p $servicedst >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1278     [[ ! -d $servicedst/etc ]] && mkdir -p $servicedst/etc >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1279     echo "<?php define('WEBROOT','${webdirdest}');" > $servicedst/etc/config.php
 1280     startInitScript
 1281 }
 1282 configureNFS() {
 1283     dots "Setting up exports file"
 1284     if [[ $blexports != 1 ]]; then
 1285         echo "Skipped"
 1286     else
 1287         mv -fv "${nfsconfig}" "${nfsconfig}.${timestamp}" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1288         echo -e "$storageLocation *(ro,sync,no_wdelay,no_subtree_check,insecure_locks,no_root_squash,insecure,fsid=0)\n$storageLocation/dev *(rw,async,no_wdelay,no_subtree_check,no_root_squash,insecure,fsid=1)" > "$nfsconfig"
 1289         diffconfig "${nfsconfig}"
 1290         errorStat $?
 1291         dots "Setting up and starting RPCBind"
 1292         if [[ $systemctl == yes ]]; then
 1293             systemctl enable rpcbind.service >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1294             systemctl stop rpcbind.service >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1295             sleep 2
 1296             systemctl start rpcbind.service >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1297             sleep 2
 1298             systemctl status rpcbind.service >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1299         else
 1300             case $osid in
 1301                 1)
 1302                     chkconfig rpcbind on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1303                     $initdpath/rpcbind stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1304                     sleep 2
 1305                     $initdpath/rpcbind start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1306                     sleep 2
 1307                     $initdpath/rpcbind status >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1308                     ;;
 1309             esac
 1310         fi
 1311         errorStat $?
 1312         dots "Setting up and starting NFS Server..."
 1313         for nfsItem in $nfsservice; do
 1314             if [[ $systemctl == yes ]]; then
 1315                 systemctl enable $nfsItem >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1316                 systemctl stop $nfsItem >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1317                 sleep 2
 1318                 systemctl start $nfsItem >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1319                 sleep 2
 1320                 systemctl status $nfsItem >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1321             else
 1322                 case $osid in
 1323                     1)
 1324                         chkconfig $nfsItem on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1325                         $initdpath/$nfsItem stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1326                         sleep 2
 1327                         $initdpath/$nfsItem start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1328                         sleep 2
 1329                         $initdpath/$nfsItem status >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1330                         ;;
 1331                     2)
 1332                         sysv-rc-conf $nfsItem on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1333                         $initdpath/nfs-kernel-server stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1334                         sleep 2
 1335                         $initdpath/nfs-kernel-server start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1336                         sleep 2
 1337                         ;;
 1338                 esac
 1339             fi
 1340             [[ $? -eq 0 ]] && break
 1341         done
 1342         errorStat $?
 1343     fi
 1344 }
 1345 configureSnapins() {
 1346     dots "Setting up FOG Snapins"
 1347     mkdir -p $snapindir >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1348     if [[ -d $snapindir ]]; then
 1349         chmod -R 777 $snapindir
 1350         chown -R $username:$apacheuser $snapindir
 1351     fi
 1352     errorStat $?
 1353 }
 1354 configureUsers() {
 1355     userexists=0
 1356     [[ -z $username || "x$username" = "xfog" ]] && username='fogproject'
 1357     dots "Setting up $username user"
 1358     getent passwd $username > /dev/null
 1359     if [[ $? -eq 0 ]]; then
 1360         if [[ ! -f "$fogprogramdir/.fogsettings" && ! -x /home/$username/warnfogaccount.sh ]]; then
 1361             echo "Already exists"
 1362             echo
 1363             echo "The account \"$username\" already exists but this seems to be a"
 1364             echo "fresh install. We highly recommend to NOT creating this account"
 1365             echo "beforehand as it is supposed to be a system account not meant"
 1366             echo "to be used to login and work on the machine!"
 1367             echo
 1368             echo "Please remove the account \"$username\" manually before running"
 1369             echo "the installer again. Run: userdel $username"
 1370             echo
 1371             exit 1
 1372         else
 1373             lastlog -u $username | tail -n -1 | grep "\*\*.*\*\*" > /dev/null 2>&1
 1374             if [[ $? -eq 1 ]]; then
 1375                 echo "Already exists"
 1376                 echo
 1377                 echo "The account \"$username\" already exists and has been used to"
 1378                 echo "logon and work on this machine. We highly recommend you NOT"
 1379                 echo "use this account for your work as it is supposed to be a"
 1380                 echo "system account!"
 1381                 echo
 1382                 echo "Please remove the account \"$username\" manually before running"
 1383                 echo "the installer again. Run: userdel $username"
 1384                 echo
 1385                 exit 1
 1386             fi
 1387             echo "Skipped"
 1388         fi
 1389     else
 1390         useradd -s "/bin/bash" -d "/home/${username}" -m ${username} >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1391         errorStat $?
 1392     fi
 1393     if [[ ! -d /home/$username ]]; then
 1394         echo "# It has been noticed that your $username home folder is missing, #"
 1395         echo "#   has been deleted, or has been moved.                          #"
 1396         echo "# This may cause issues with capturing images and snapin uploads. #"
 1397         echo "# If you this move/delete was unintentional you can run:          #"
 1398         echo " userdel $username"
 1399         echo " useradd -s \"/bin/bash\" -d \"/home/$username\" -m \"$username\""
 1400         #userdel $username
 1401         #useradd -s "/bin/bash" -d "/home/${username}" -m ${username} >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1402         #errorStat $?
 1403     fi
 1404     dots "Locking $username as a system account"
 1405     chsh -s /bin/bash $username >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1406     textmessage="You seem to be using the '$username' system account to logon and work \non your FOG server system.\n\nIt's NOT recommended to use this account! Please create a new \naccount for administrative tasks.\n\nIf you re-run the installer it would reset the 'fog' account \npassword and therefore lock you out of the system!\n\nTake care, \nyour FOGproject team"
 1407     grep -q "exit 1" /home/$username/.bashrc || cat >>/home/$username/.bashrc <<EOF
 1408 
 1409 echo -e "$textmessage"
 1410 exit 1
 1411 EOF
 1412     mkdir -p /home/$username/.config/autostart/
 1413     cat >/home/$username/.config/autostart/warnfogaccount.desktop <<EOF
 1414 [Desktop Entry]
 1415 Type=Application
 1416 Name=Warn users to not use the $username account
 1417 Exec=/home/$username/warnfogaccount.sh
 1418 Comment=Warn users who use the $username system account to logon
 1419 EOF
 1420     chown -R $username:$username /home/$username/.config/
 1421     cat >/home/$username/warnfogaccount.sh <<EOF
 1422 #!/bin/bash
 1423 title="FOG system account"
 1424 text="$textmessage"
 1425 z=\$(which zenity)
 1426 x=\$(which xmessage)
 1427 n=\$(which notify-send)
 1428 if [[ -x "\$z" ]]
 1429 then
 1430     \$z --error --width=480 --text="\$text" --title="\$title"
 1431 elif [[ -x "\$x" ]]
 1432 then
 1433     echo -e "\$text" | \$x -center -file -
 1434 else
 1435     \$n -u critical "\$title" "\$(echo \$text | sed -e 's/ \\n/ /g')"
 1436 fi
 1437 EOF
 1438     chmod 755 /home/$username/warnfogaccount.sh
 1439     chown $username:$username /home/$username/warnfogaccount.sh
 1440     errorStat $?
 1441     dots "Setting up $username password"
 1442     if [[ -z $password ]]; then
 1443         [[ -f $webdirdest/lib/fog/config.class.php ]] && password=$(awk -F '"' -e '/TFTP_FTP_PASSWORD/,/);/{print $2}' $webdirdest/lib/fog/config.class.php | grep -v "^$")
 1444     fi
 1445     if [[ -n "$(checkPasswordChars)" ]]
 1446     then
 1447         echo "Failed"
 1448         echo "# The fog system account password includes characters we cannot properly"
 1449         echo "# handle. Please remove the following character(s) from the password in"
 1450         echo "# your .fogsettings file before re-running the installer: $passcheck"
 1451         exit 1
 1452     fi
 1453     cnt=0
 1454     ret=999
 1455     while [[ $ret -ne 0 && $cnt -lt 10 ]]
 1456     do
 1457         [[ -z $password || $ret -ne 999 ]] && password=$(generatePassword 20)
 1458         echo -e "$password\n$password" | passwd $username >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1459         ret=$?
 1460         let cnt+=1
 1461     done
 1462     errorStat $ret
 1463     unset cnt
 1464     unset ret
 1465 }
 1466 linkOptFogDir() {
 1467     if [[ ! -h /var/log/fog ]]; then
 1468         dots "Linking FOG Logs to Linux Logs"
 1469         ln -s /opt/fog/log /var/log/fog >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1470         errorStat $?
 1471     fi
 1472     if [[ ! -h /etc/fog ]]; then
 1473         dots "Linking FOG Service config /etc"
 1474         ln -s /opt/fog/service/etc /etc/fog >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1475         errorStat $?
 1476     fi
 1477     local element='httpd'
 1478     [[ $osid -eq 2 ]] && element='apache2'
 1479     chmod -R 755 /var/log/$element >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1480     for i in $(find /var/log/ -type d -name 'php*fpm*' 2>>$workingdir/error_logs/fog_error_${version}.log); do
 1481         chmod -R 755 $i >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1482     done
 1483     for i in $(find /var/log/ -type f -name 'php*fpm*' 2>>$workingdir/error_logs/fog_error_${version}.log); do
 1484         chmod -R 755 $i >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1485     done
 1486 }
 1487 configureStorage() {
 1488     dots "Setting up storage"
 1489     [[ ! -d $storageLocation ]] && mkdir $storageLocation >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1490     [[ ! -f $storageLocation/.mntcheck ]] && touch $storageLocation/.mntcheck >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1491     [[ ! -d $storageLocation/postdownloadscripts ]] && mkdir $storageLocation/postdownloadscripts >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1492     if [[ ! -f $storageLocation/postdownloadscripts/fog.postdownload ]]; then
 1493         echo "#!/bin/bash" >"$storageLocation/postdownloadscripts/fog.postdownload"
 1494         echo "## This file serves as a starting point to call your custom postimaging scripts." >>"$storageLocation/postdownloadscripts/fog.postdownload"
 1495         echo "## <SCRIPTNAME> should be changed to the script you're planning to use." >>"$storageLocation/postdownloadscripts/fog.postdownload"
 1496         echo "## Syntax of post download scripts are" >>"$storageLocation/postdownloadscripts/fog.postdownload"
 1497         echo "#. \${postdownpath}<SCRIPTNAME>" >> "$storageLocation/postdownloadscripts/fog.postdownload"
 1498     fi
 1499     [[ ! -d $storageLocationCapture ]] && mkdir $storageLocationCapture >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1500     [[ ! -f $storageLocationCapture/.mntcheck ]] && touch $storageLocationCapture/.mntcheck >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1501     [[ ! -d $storageLocationCapture/postinitscripts ]] && mkdir $storageLocationCapture/postinitscripts >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1502     if [[ ! -f $storageLocationCapture/postinitscripts/fog.postinit ]]; then
 1503         echo "#!/bin/bash" >"$storageLocationCapture/postinitscripts/fog.postinit"
 1504         echo "## This file serves as a starting point to call your custom pre-imaging/post init loading scripts." >>"$storageLocationCapture/postinitscripts/fog.postinit"
 1505         echo "## <SCRIPTNAME> should be changed to the script you're planning to use." >>"$storageLocationCapture/postinitscripts/fog.postinit"
 1506         echo "## Syntax of post init scripts are" >>"$storageLocationCapture/postinitscripts/fog.postinit"
 1507         echo "#. \${postinitpath}<SCRIPTNAME>" >>"$storageLocationCapture/postinitscripts/fog.postinit"
 1508     else
 1509         (head -1 "$storageLocationCapture/postinitscripts/fog.postinit" | grep -q '^#!/bin/bash') || sed -i '1i#!/bin/bash' "$storageLocationCapture/postinitscripts/fog.postinit" >/dev/null 2>&1
 1510     fi
 1511     chmod -R 777 $storageLocation $storageLocationCapture >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1512     chown -R $username $storageLocation $storageLocationCapture >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1513     errorStat $?
 1514 }
 1515 clearScreen() {
 1516     clear
 1517 }
 1518 writeUpdateFile() {
 1519     tmpDte=$(date +%c)
 1520     replace='s/[]"\/$&*.^|[]/\\&/g';
 1521     escversion=$(echo $version | sed -e $replace)
 1522     esctmpDte=$(echo $tmpDate | sed -e $replace)
 1523     escipaddress=$(echo $ipaddress | sed -e $replace)
 1524     escinterface=$(echo $interface | sed -e $replace)
 1525     escsubmask=$(echo $submask | sed -e $replace)
 1526     eschostname=$(echo $hostname | sed -e $replace)
 1527     escrouteraddress=$(echo $routeraddress | sed -e $replace)
 1528     escplainrouter=$(echo $plainrouter | sed -e $replace)
 1529     escdnsaddress=$(echo $dnsaddress | sed -e $replace)
 1530     escpassword=$(echo $password | sed -e $replace)
 1531     escosid=$(echo $osid | sed -e $replace)
 1532     escosname=$(echo $osname | sed -e $replace)
 1533     escdodhcp=$(echo $dodhcp | sed -e $replace)
 1534     escbldhcp=$(echo $bldhcp | sed -e $replace)
 1535     escdhcpd=$(echo $dhcpd | sed -e $replace)
 1536     escblexports=$(echo $blexports | sed -e $replace)
 1537     escinstalltype=$(echo $installtype | sed -e $replace)
 1538     escsnmysqluser=$(echo $snmysqluser | sed -e $replace)
 1539     escsnmysqlpass=$(echo "$snmysqlpass" | sed -e s/\'/\'\"\'\"\'/g)  # replace every ' with '"'"' for full bash escaping
 1540     sedescsnmysqlpass=$(echo "$escsnmysqlpass" | sed -e 's/[\&/]/\\&/g')  # then prefix every \ & and / with \ for sed escaping
 1541     escsnmysqlhost=$(echo $snmysqlhost | sed -e $replace)
 1542     escmysqldbname=$(echo $mysqldbname | sed -e $replace)
 1543     escinstalllang=$(echo $installlang | sed -e $replace)
 1544     escstorageLocation=$(echo $storageLocation | sed -e $replace)
 1545     escfogupdateloaded=$(echo $fogupdateloaded | sed -e $replace)
 1546     escusername=$(echo $username | sed -e $replace)
 1547     escdocroot=$(echo $docroot | sed -e $replace)
 1548     escwebroot=$(echo $webroot | sed -e $replace)
 1549     esccaCreated=$(echo $caCreated | sed -e $replace)
 1550     eschttpproto=$(echo $httpproto | sed -e $replace)
 1551     escstartrange=$(echo $startrange | sed -e $replace)
 1552     escendrange=$(echo $endrange | sed -e $replace)
 1553     escbootfilename=$(echo $bootfilename | sed -e $replace)
 1554     escpackages=$(echo $packages | sed -e $replace)
 1555     escnoTftpBuild=$(echo $noTftpBuild | sed -e $replace)
 1556     escsslpath=$(echo $sslpath | sed -e $replace)
 1557     escbackupPath=$(echo $backupPath | sed -e $replace)
 1558     escarmsupport=$(echo $sarmsupport | sed -e $replace)
 1559     escphp_ver=$(echo $php_ver | sed -e $replace)
 1560     escphp_verAdds=$(echo $php_verAdds | sed -e $replace)
 1561     escsslprivkey=$(echo $sslprivkey | sed -e $replace)
 1562     [[ -z $copybackold || $copybackold -lt 1 ]] && copybackold=0
 1563     if [[ -f $fogprogramdir/.fogsettings ]]; then
 1564         grep -q "^## Start of FOG Settings" $fogprogramdir/.fogsettings || grep -q "^## Version:.*" $fogprogramdir/.fogsettings
 1565         if [[ $? == 0 ]]; then
 1566             grep -q "^## Version:.*$" $fogprogramdir/.fogsettings && \
 1567                 sed -i "s/^## Version:.*/## Version: $escversion/g" $fogprogramdir/.fogsettings || \
 1568                 echo "## Version: $version" >> $fogprogramdir/.fogsettings
 1569             grep -q "ipaddress=" $fogprogramdir/.fogsettings && \
 1570                 sed -i "s/ipaddress=.*/ipaddress='$escipaddress'/g" $fogprogramdir/.fogsettings || \
 1571                 echo "ipaddress='$ipaddress'" >> $fogprogramdir/.fogsettings
 1572             grep -q "copybackold=" $fogprogramdir/.fogsettings && \
 1573                 sed -i "s/copybackold=.*/copybackold='$copybackold'/g" $fogprogramdir/.fogsettings || \
 1574                 echo "copybackold='$copybackold'" >> $fogprogramdir/.fogsettings
 1575             grep -q "interface=" $fogprogramdir/.fogsettings && \
 1576                 sed -i "s/interface=.*/interface='$escinterface'/g" $fogprogramdir/.fogsettings || \
 1577                 echo "interface='$interface'" >> $fogprogramdir/.fogsettings
 1578             grep -q "submask=" $fogprogramdir/.fogsettings && \
 1579                 sed -i "s/submask=.*/submask='$escsubmask'/g" $fogprogramdir/.fogsettings || \
 1580                 echo "submask='$submask'" >> $fogprogramdir/.fogsettings
 1581             grep -q "hostname=" $fogprogramdir/.fogsettings && \
 1582                 sed -i "s/hostname=.*/hostname='$eschostname'/g" $fogprogramdir/.fogsettings || \
 1583                 echo "hostname='$hostname'" >> $fogprogramdir/.fogsettings
 1584             grep -q "routeraddress=" $fogprogramdir/.fogsettings && \
 1585                 sed -i "s/routeraddress=.*/routeraddress='$escrouteraddress'/g" $fogprogramdir/.fogsettings || \
 1586                 echo "routeraddress='$routeraddress'" >> $fogprogramdir/.fogsettings
 1587             grep -q "plainrouter=" $fogprogramdir/.fogsettings && \
 1588                 sed -i "s/plainrouter=.*/plainrouter='$escplainrouter'/g" $fogprogramdir/.fogsettings || \
 1589                 echo "plainrouter='$plainrouter'" >> $fogprogramdir/.fogsettings
 1590             grep -q "dnsaddress=" $fogprogramdir/.fogsettings && \
 1591                 sed -i "s/dnsaddress=.*/dnsaddress='$escdnsaddress'/g" $fogprogramdir/.fogsettings || \
 1592                 echo "dnsaddress='$dnsaddress'" >> $fogprogramdir/.fogsettings
 1593             grep -q "password=" $fogprogramdir/.fogsettings && \
 1594                 sed -i "s/password=.*/password='$escpassword'/g" $fogprogramdir/.fogsettings || \
 1595                 echo "password='$password'" >> $fogprogramdir/.fogsettings
 1596             grep -q "osid=" $fogprogramdir/.fogsettings && \
 1597                 sed -i "s/osid=.*/osid='$osid'/g" $fogprogramdir/.fogsettings || \
 1598                 echo "osid='$osid'" >> $fogprogramdir/.fogsettings
 1599             grep -q "osname=" $fogprogramdir/.fogsettings && \
 1600                 sed -i "s/osname=.*/osname='$escosname'/g" $fogprogramdir/.fogsettings || \
 1601                 echo "osname='$osname'" >> $fogprogramdir/.fogsettings
 1602             grep -q "dodhcp=" $fogprogramdir/.fogsettings && \
 1603                 sed -i "s/dodhcp=.*/dodhcp='$escdodhcp'/g" $fogprogramdir/.fogsettings || \
 1604                 echo "dodhcp='$dodhcp'" >> $fogprogramdir/.fogsettings
 1605             grep -q "bldhcp=" $fogprogramdir/.fogsettings && \
 1606                 sed -i "s/bldhcp=.*/bldhcp='$escbldhcp'/g" $fogprogramdir/.fogsettings || \
 1607                 echo "bldhcp='$bldhcp'" >> $fogprogramdir/.fogsettings
 1608             grep -q "dhcpd=" $fogprogramdir/.fogsettings && \
 1609                 sed -i "s/dhcpd=.*/dhcpd='$escdhcpd'/g" $fogprogramdir/.fogsettings || \
 1610                 echo "dhcpd='$dhcpd'" >> $fogprogramdir/.fogsettings
 1611             grep -q "blexports=" $fogprogramdir/.fogsettings && \
 1612                 sed -i "s/blexports=.*/blexports='$escblexports'/g" $fogprogramdir/.fogsettings || \
 1613                 echo "blexports='$blexports'" >> $fogprogramdir/.fogsettings
 1614             grep -q "installtype=" $fogprogramdir/.fogsettings && \
 1615                 sed -i "s/installtype=.*/installtype='$escinstalltype'/g" $fogprogramdir/.fogsettings || \
 1616                 echo "installtype='$installtype'" >> $fogprogramdir/.fogsettings
 1617             grep -q "snmysqluser=" $fogprogramdir/.fogsettings && \
 1618                 sed -i "s/snmysqluser=.*/snmysqluser='$escsnmysqluser'/g" $fogprogramdir/.fogsettings || \
 1619                 echo "snmysqluser='$snmysqluser'" >> $fogprogramdir/.fogsettings
 1620             grep -q "snmysqlpass=" $fogprogramdir/.fogsettings && \
 1621                 sed -i "s/snmysqlpass=.*/snmysqlpass='$sedescsnmysqlpass'/g" $fogprogramdir/.fogsettings || \
 1622                 echo "snmysqlpass='$escsnmysqlpass'" >> $fogprogramdir/.fogsettings
 1623             grep -q "snmysqlhost=" $fogprogramdir/.fogsettings && \
 1624                 sed -i "s/snmysqlhost=.*/snmysqlhost='$escsnmysqlhost'/g" $fogprogramdir/.fogsettings || \
 1625                 echo "snmysqlhost='$snmysqlhost'" >> $fogprogramdir/.fogsettings
 1626             grep -q "mysqldbname=" $fogprogramdir/.fogsettings && \
 1627                 sed -i "s/mysqldbname=.*/mysqldbname='$escmysqldbname'/g" $fogprogramdir/.fogsettings || \
 1628                 echo "mysqldbname='$mysqldbname'" >> $fogprogramdir/.fogsettings
 1629             grep -q "installlang=" $fogprogramdir/.fogsettings && \
 1630                 sed -i "s/installlang=.*/installlang='$escinstalllang'/g" $fogprogramdir/.fogsettings || \
 1631                 echo "installlang='$installlang'" >> $fogprogramdir/.fogsettings
 1632             grep -q "storageLocation=" $fogprogramdir/.fogsettings && \
 1633                 sed -i "s/storageLocation=.*/storageLocation='$escstorageLocation'/g" $fogprogramdir/.fogsettings || \
 1634                 echo "storageLocation='$storageLocation'" >> $fogprogramdir/.fogsettings
 1635             grep -q "fogupdateloaded=" $fogprogramdir/.fogsettings && \
 1636                 sed -i "s/fogupdateloaded=.*/fogupdateloaded=$escfogupdateloaded/g" $fogprogramdir/.fogsettings || \
 1637                 echo "fogupdateloaded=$fogupdateloaded" >> $fogprogramdir/.fogsettings
 1638             grep -q "storageftpuser=" $fogprogramdir/.fogsettings && \
 1639                 sed -i "/storageftpuser=/d" $fogprogramdir/.fogsettings
 1640             grep -q "storageftppass=" $fogprogramdir/.fogsettings && \
 1641                 sed -i "/storageftppass=/d" $fogprogramdir/.fogsettings
 1642             grep -q "username=" $fogprogramdir/.fogsettings && \
 1643                 sed -i "s/username=.*/username='$escusername'/g" $fogprogramdir/.fogsettings || \
 1644                 echo "username='$username'" >> $fogprogramdir/.fogsettings
 1645             grep -q "docroot=" $fogprogramdir/.fogsettings && \
 1646                 sed -i "s/docroot=.*/docroot='$escdocroot'/g" $fogprogramdir/.fogsettings || \
 1647                 echo "docroot='$docroot'" >> $fogprogramdir/.fogsettings
 1648             grep -q "webroot=" $fogprogramdir/.fogsettings && \
 1649                 sed -i "s/webroot=.*/webroot='$escwebroot'/g" $fogprogramdir/.fogsettings || \
 1650                 echo "webroot='$webroot'" >> $fogprogramdir/.fogsettings
 1651             grep -q "caCreated=" $fogprogramdir/.fogsettings && \
 1652                 sed -i "s/caCreated=.*/caCreated='$esccaCreated'/g" $fogprogramdir/.fogsettings || \
 1653                 echo "caCreated='$caCreated'" >> $fogprogramdir/.fogsettings
 1654             grep -q "httpproto=" $fogprogramdir/.fogsettings && \
 1655                 sed -i "s/httpproto=.*/httpproto='$eschttpproto'/g" $fogprogramdir/.fogsettings || \
 1656                 echo "httpproto='$httpproto'" >> $fogprogramdir/.fogsettings
 1657             grep -q "startrange=" $fogprogramdir/.fogsettings && \
 1658                 sed -i "s/startrange=.*/startrange='$escstartrange'/g" $fogprogramdir/.fogsettings || \
 1659                 echo "startrange='$startrange'" >> $fogprogramdir/.fogsettings
 1660             grep -q "endrange=" $fogprogramdir/.fogsettings && \
 1661                 sed -i "s/endrange=.*/endrange='$escendrange'/g" $fogprogramdir/.fogsettings || \
 1662                 echo "endrange='$endrange'" >> $fogprogramdir/.fogsettings
 1663             grep -q "bootfilename=" $fogprogramdir/.fogsettings && \
 1664                 sed -i "s/bootfilename=.*/bootfilename='$escbootfilename'/g" $fogprogramdir/.fogsettings || \
 1665                 echo "bootfilename='$bootfilename'" >> $fogprogramdir/.fogsettings
 1666             grep -q "packages=" $fogprogramdir/.fogsettings && \
 1667                 sed -i "s/packages=.*/packages='$escpackages'/g" $fogprogramdir/.fogsettings || \
 1668                 echo "packages='$packages'" >> $fogprogramdir/.fogsettings
 1669             grep -q "noTftpBuild=" $fogprogramdir/.fogsettings && \
 1670                 sed -i "s/noTftpBuild=.*/noTftpBuild='$escnoTftpBuild'/g" $fogprogramdir/.fogsettings || \
 1671                 echo "noTftpBuild='$noTftpBuild'" >> $fogprogramdir/.fogsettings
 1672             grep -q "notpxedefaultfile=" $fogprogramdir/.fogsettings && \
 1673                 sed -i "/notpxedefaultfile=.*$/d" $fogprogramdir/.fogsettings
 1674             grep -q "sslpath=" $fogprogramdir/.fogsettings && \
 1675                 sed -i "s/sslpath=.*/sslpath='$escsslpath'/g" $fogprogramdir/.fogsettings || \
 1676                 echo "sslpath='$sslpath'" >> $fogprogramdir/.fogsettings
 1677             grep -q "backupPath=" $fogprogramdir/.fogsettings && \
 1678                 sed -i "s/backupPath=.*/backupPath='$escbackupPath'/g" $fogprogramdir/.fogsettings || \
 1679                 echo "backupPath='$backupPath'" >> $fogprogramdir/.fogsettings
 1680             grep -q "armsupport=" $fogprogramdir/.fogsettings && \
 1681                 sed -i "s/armsupport=.*/armsupport='$escarmsupport'/g" $fogprogramdir/.fogsettings || \
 1682                 echo "armsupport='$armsupport'" >> $fogprogramdir/.fogsettings
 1683             grep -q "php_ver=" $fogprogramdir/.fogsettings && \
 1684                 sed -i "s/php_ver=.*/php_ver='$php_ver'/g" $fogprogramdir/.fogsettings || \
 1685                 echo "php_ver='$php_ver'" >> $fogprogramdir/.fogsettings
 1686             grep -q "php_verAdds=" $fogprogramdir/.fogsettings && \
 1687                 sed -i "s/php_verAdds=.*/php_verAdds='$php_verAdds'/g" $fogprogramdir/.fogsettings || \
 1688                 echo "php_verAdds='$php_verAdds'" >> $fogprogramdir/.fogsettings
 1689             grep -q "sslprivkey=" $fogprogramdir/.fogsettings && \
 1690                 sed -i "s/sslprivkey=.*/sslprivkey='$escsslprivkey'/g" $fogprogramdir/.fogsettings || \
 1691                 echo "sslprivkey='$sslprivkey'" >> $fogprogramdir/.fogsettings
 1692         else
 1693             echo "## Start of FOG Settings" > "$fogprogramdir/.fogsettings"
 1694             echo "## Created by the FOG Installer" >> "$fogprogramdir/.fogsettings"
 1695             echo "## Find more information about this file in the FOG Project wiki:" >> "$fogprogramdir/.fogsettings"
 1696             echo "##     https://wiki.fogproject.org/wiki/index.php?title=.fogsettings" >> "$fogprogramdir/.fogsettings"
 1697             echo "## Version: $version" >> "$fogprogramdir/.fogsettings"
 1698             echo "## Install time: $tmpDte" >> "$fogprogramdir/.fogsettings"
 1699             echo "ipaddress='$ipaddress'" >> "$fogprogramdir/.fogsettings"
 1700             echo "copybackold='$copybackold'" >> "$fogprogramdir/.fogsettings"
 1701             echo "interface='$interface'" >> "$fogprogramdir/.fogsettings"
 1702             echo "submask='$submask'" >> "$fogprogramdir/.fogsettings"
 1703             echo "hostname='$hostname'" >> "$fogprogramdir/.fogsettings"
 1704             echo "routeraddress='$routeraddress'" >> "$fogprogramdir/.fogsettings"
 1705             echo "plainrouter='$plainrouter'" >> "$fogprogramdir/.fogsettings"
 1706             echo "dnsaddress='$dnsaddress'" >> "$fogprogramdir/.fogsettings"
 1707             echo "username='$username'" >> "$fogprogramdir/.fogsettings"
 1708             echo "password='$password'" >> "$fogprogramdir/.fogsettings"
 1709             echo "osid='$osid'" >> "$fogprogramdir/.fogsettings"
 1710             echo "osname='$osname'" >> "$fogprogramdir/.fogsettings"
 1711             echo "dodhcp='$dodhcp'" >> "$fogprogramdir/.fogsettings"
 1712             echo "bldhcp='$bldhcp'" >> "$fogprogramdir/.fogsettings"
 1713             echo "dhcpd='$dhcpd'" >> "$fogprogramdir/.fogsettings"
 1714             echo "blexports='$blexports'" >> "$fogprogramdir/.fogsettings"
 1715             echo "installtype='$installtype'" >> "$fogprogramdir/.fogsettings"
 1716             echo "snmysqluser='$snmysqluser'" >> "$fogprogramdir/.fogsettings"
 1717             echo "snmysqlpass='$escsnmysqlpass'" >> "$fogprogramdir/.fogsettings"
 1718             echo "snmysqlhost='$snmysqlhost'" >> "$fogprogramdir/.fogsettings"
 1719             echo "mysqldbname='$mysqldbname'" >> "$fogprogramdir/.fogsettings"
 1720             echo "installlang='$installlang'" >> "$fogprogramdir/.fogsettings"
 1721             echo "storageLocation='$storageLocation'" >> "$fogprogramdir/.fogsettings"
 1722             echo "fogupdateloaded=1" >> "$fogprogramdir/.fogsettings"
 1723             echo "docroot='$docroot'" >> "$fogprogramdir/.fogsettings"
 1724             echo "webroot='$webroot'" >> "$fogprogramdir/.fogsettings"
 1725             echo "caCreated='$caCreated'" >> "$fogprogramdir/.fogsettings"
 1726             echo "httpproto='$httpproto'" >> "$fogprogramdir/.fogsettings"
 1727             echo "startrange='$startrange'" >> "$fogprogramdir/.fogsettings"
 1728             echo "endrange='$endrange'" >> "$fogprogramdir/.fogsettings"
 1729             echo "bootfilename='$bootfilename'" >> "$fogprogramdir/.fogsettings"
 1730             echo "packages='$packages'" >> "$fogprogramdir/.fogsettings"
 1731             echo "noTftpBuild='$noTftpBuild'" >> "$fogprogramdir/.fogsettings"
 1732             echo "sslpath='$sslpath'" >> "$fogprogramdir/.fogsettings"
 1733             echo "backupPath='$backupPath'" >> "$fogprogramdir/.fogsettings"
 1734             echo "armsupport='$armsupport'" >> "$fogprogramdir/.fogsettings"
 1735             echo "php_ver='$php_ver'" >> "$fogprogramdir/.fogsettings"
 1736             echo "php_verAdds='$php_verAdds'" >> "$fogprogramdir/.fogsettings"
 1737             echo "sslprivkey='$sslprivkey'" >> $fogprogramdir/.fogsettings
 1738             echo "## End of FOG Settings" >> "$fogprogramdir/.fogsettings"
 1739         fi
 1740     else
 1741         echo "## Start of FOG Settings" > "$fogprogramdir/.fogsettings"
 1742         echo "## Created by the FOG Installer" >> "$fogprogramdir/.fogsettings"
 1743         echo "## Find more information about this file in the FOG Project wiki:" >> "$fogprogramdir/.fogsettings"
 1744         echo "##     https://wiki.fogproject.org/wiki/index.php?title=.fogsettings" >> "$fogprogramdir/.fogsettings"
 1745         echo "## Version: $version" >> "$fogprogramdir/.fogsettings"
 1746         echo "## Install time: $tmpDte" >> "$fogprogramdir/.fogsettings"
 1747         echo "ipaddress='$ipaddress'" >> "$fogprogramdir/.fogsettings"
 1748         echo "copybackold='$copybackold'" >> "$fogprogramdir/.fogsettings"
 1749         echo "interface='$interface'" >> "$fogprogramdir/.fogsettings"
 1750         echo "submask='$submask'" >> "$fogprogramdir/.fogsettings"
 1751         echo "hostname='$hostname'" >> "$fogprogramdir/.fogsettings"
 1752         echo "routeraddress='$routeraddress'" >> "$fogprogramdir/.fogsettings"
 1753         echo "plainrouter='$plainrouter'" >> "$fogprogramdir/.fogsettings"
 1754         echo "dnsaddress='$dnsaddress'" >> "$fogprogramdir/.fogsettings"
 1755         echo "username='$username'" >> "$fogprogramdir/.fogsettings"
 1756         echo "password='$password'" >> "$fogprogramdir/.fogsettings"
 1757         echo "osid='$osid'" >> "$fogprogramdir/.fogsettings"
 1758         echo "osname='$osname'" >> "$fogprogramdir/.fogsettings"
 1759         echo "dodhcp='$dodhcp'" >> "$fogprogramdir/.fogsettings"
 1760         echo "bldhcp='$bldhcp'" >> "$fogprogramdir/.fogsettings"
 1761         echo "dhcpd='$dhcpd'" >> "$fogprogramdir/.fogsettings"
 1762         echo "blexports='$blexports'" >> "$fogprogramdir/.fogsettings"
 1763         echo "installtype='$installtype'" >> "$fogprogramdir/.fogsettings"
 1764         echo "snmysqluser='$snmysqluser'" >> "$fogprogramdir/.fogsettings"
 1765         echo "snmysqlpass='$escsnmysqlpass'" >> "$fogprogramdir/.fogsettings"
 1766         echo "snmysqlhost='$snmysqlhost'" >> "$fogprogramdir/.fogsettings"
 1767         echo "mysqldbname='$mysqldbname'" >> "$fogprogramdir/.fogsettings"
 1768         echo "installlang='$installlang'" >> "$fogprogramdir/.fogsettings"
 1769         echo "storageLocation='$storageLocation'" >> "$fogprogramdir/.fogsettings"
 1770         echo "fogupdateloaded=1" >> "$fogprogramdir/.fogsettings"
 1771         echo "docroot='$docroot'" >> "$fogprogramdir/.fogsettings"
 1772         echo "webroot='$webroot'" >> "$fogprogramdir/.fogsettings"
 1773         echo "caCreated='$caCreated'" >> "$fogprogramdir/.fogsettings"
 1774         echo "httpproto='$httpproto'" >> "$fogprogramdir/.fogsettings"
 1775         echo "startrange='$startrange'" >> "$fogprogramdir/.fogsettings"
 1776         echo "endrange='$endrange'" >> "$fogprogramdir/.fogsettings"
 1777         echo "bootfilename='$bootfilename'" >> "$fogprogramdir/.fogsettings"
 1778         echo "packages='$packages'" >> "$fogprogramdir/.fogsettings"
 1779         echo "noTftpBuild='$noTftpBuild'" >> "$fogprogramdir/.fogsettings"
 1780         echo "sslpath='$sslpath'" >> "$fogprogramdir/.fogsettings"
 1781         echo "backupPath='$backupPath'" >> "$fogprogramdir/.fogsettings"
 1782         echo "armsupport='$armsupport'" >> "$fogprogramdir/.fogsettings"
 1783         echo "php_ver='$php_ver'" >> "$fogprogramdir/.fogsettings"
 1784         echo "php_verAdds='$php_verAdds'" >> "$fogprogramdir/.fogsettings"
 1785         echo "sslprivkey='$sslprivkey'" >> $fogprogramdir/.fogsettings
 1786         echo "## End of FOG Settings" >> "$fogprogramdir/.fogsettings"
 1787     fi
 1788 }
 1789 displayBanner() {
 1790     echo
 1791     echo
 1792     echo "   +------------------------------------------+"
 1793     echo "   |     ..#######:.    ..,#,..     .::##::.  |"
 1794     echo "   |.:######          .:;####:......;#;..     |"
 1795     echo "   |...##...        ...##;,;##::::.##...      |"
 1796     echo "   |   ,#          ...##.....##:::##     ..:: |"
 1797     echo "   |   ##    .::###,,##.   . ##.::#.:######::.|"
 1798     echo "   |...##:::###::....#. ..  .#...#. #...#:::. |"
 1799     echo "   |..:####:..    ..##......##::##  ..  #     |"
 1800     echo "   |    #  .      ...##:,;##;:::#: ... ##..   |"
 1801     echo "   |   .#  .       .:;####;::::.##:::;#:..    |"
 1802     echo "   |    #                     ..:;###..       |"
 1803     echo "   |                                          |"
 1804     echo "   +------------------------------------------+"
 1805     echo "   |      Free Computer Imaging Solution      |"
 1806     echo "   +------------------------------------------+"
 1807     echo "   |  Credits: http://fogproject.org/Credits  |"
 1808     echo "   |       http://fogproject.org/Credits      |"
 1809     echo "   |       Released under GPL Version 3       |"
 1810     echo "   +------------------------------------------+"
 1811     echo
 1812     echo
 1813 }
 1814 createSSLCA() {
 1815     if [[ -z $sslpath ]]; then
 1816         [[ -d /opt/fog/snapins/CA && -d /opt/fog/snapins/ssl ]] && mv /opt/fog/snapins/CA /opt/fog/snapins/ssl/
 1817         sslpath='/opt/fog/snapins/ssl/'
 1818     fi
 1819     if [[ $recreateCA == yes || $caCreated != yes || ! -e $sslpath/CA || ! -e $sslpath/CA/.fogCA.key ]]; then
 1820         mkdir -p $sslpath/CA >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1821         dots "Creating SSL CA"
 1822         openssl genrsa -out $sslpath/CA/.fogCA.key 4096 >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1823         openssl req -x509 -new -sha512 -nodes -key $sslpath/CA/.fogCA.key -days 3650 -out $sslpath/CA/.fogCA.pem >>$workingdir/error_logs/fog_error_${version}.log 2>&1 << EOF
 1824 .
 1825 .
 1826 .
 1827 .
 1828 .
 1829 FOG Server CA
 1830 .
 1831 EOF
 1832         errorStat $?
 1833     fi
 1834     [[ -z $sslprivkey ]] && sslprivkey="$sslpath/.srvprivate.key"
 1835     if [[ $recreateKeys == yes || $recreateCA == yes || $caCreated != yes || ! -e $sslpath || ! -e $sslprivkey ]]; then
 1836         dots "Creating SSL Private Key"
 1837         if [[ $(validip $ipaddress) -ne 0 ]]; then
 1838             echo -e "\n"
 1839             echo "  You seem to be using a DNS name instead of an IP address."
 1840             echo "  This would cause an error when generating SSL key and certs"
 1841             echo "  and so we will stop here! Please adjust variable 'ipaddress'"
 1842             echo "  in .fogsettings file if this is an update and make sure you"
 1843             echo "  provide an IP address when re-running the installer."
 1844             exit 1
 1845         fi
 1846         mkdir -p $sslpath >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1847         openssl genrsa -out $sslprivkey 4096 >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1848         cat > $sslpath/req.cnf << EOF
 1849 [req]
 1850 distinguished_name = req_distinguished_name
 1851 req_extensions = v3_req
 1852 prompt = yes
 1853 [req_distinguished_name]
 1854 CN = $ipaddress
 1855 [v3_req]
 1856 subjectAltName = @alt_names
 1857 [alt_names]
 1858 IP.1 = $ipaddress
 1859 DNS.1 = $hostname
 1860 EOF
 1861         openssl req -new -sha512 -key $sslprivkey -out $sslpath/fog.csr -config $sslpath/req.cnf >>$workingdir/error_logs/fog_error_${version}.log 2>&1 << EOF
 1862 $ipaddress
 1863 EOF
 1864         errorStat $?
 1865     fi
 1866     [[ ! -e $sslpath/.srvprivate.key ]] && ln -sf $sslprivkey $sslpath/.srvprivate.key >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1867     dots "Creating SSL Certificate"
 1868     mkdir -p $webdirdest/management/other/ssl >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1869     cat > $sslpath/ca.cnf << EOF
 1870 [v3_ca]
 1871 subjectAltName = @alt_names
 1872 [alt_names]
 1873 IP.1 = $ipaddress
 1874 DNS.1 = $hostname
 1875 EOF
 1876     openssl x509 -req -in $sslpath/fog.csr -CA $sslpath/CA/.fogCA.pem -CAkey $sslpath/CA/.fogCA.key -CAcreateserial -out $webdirdest/management/other/ssl/srvpublic.crt -days 3650 -extensions v3_ca -extfile $sslpath/ca.cnf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1877     errorStat $?
 1878     dots "Creating auth pub key and cert"
 1879     cp $sslpath/CA/.fogCA.pem $webdirdest/management/other/ca.cert.pem >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1880     openssl x509 -outform der -in $webdirdest/management/other/ca.cert.pem -out $webdirdest/management/other/ca.cert.der >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1881     errorStat $?
 1882     dots "Resetting SSL Permissions"
 1883     chown -R $apacheuser:$apacheuser $webdirdest/management/other >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1884     errorStat $?
 1885     [[ $httpproto == https ]] && sslenabled=" (SSL)" || sslenabled=" (no SSL)"
 1886     dots "Setting up Apache virtual host${sslenabled}"
 1887     case $novhost in
 1888         [Yy]|[Yy][Ee][Ss])
 1889             echo "Skipped"
 1890             ;;
 1891         *)
 1892                 if [[ $osid -eq 2 ]]; then
 1893                     a2dissite 001-fog >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1894                     a2ensite 000-default >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1895                 fi
 1896                 mv -fv "${etcconf}" "${etcconf}.${timestamp}" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1897                 echo "<VirtualHost *:80>" > "$etcconf"
 1898                 echo "    <FilesMatch \"\.php\$\">" >> "$etcconf"
 1899                 if [[ $osid -eq 1 && $OSVersion -lt 7 ]]; then
 1900                     echo "        SetHandler application/x-httpd-php" >> "$etcconf"
 1901                 else
 1902                     echo "        SetHandler \"proxy:fcgi://127.0.0.1:9000/\"" >> "$etcconf"
 1903                 fi
 1904                 echo "    </FilesMatch>" >> "$etcconf"
 1905                 echo "    ServerName $ipaddress" >> "$etcconf"
 1906                 echo "    ServerAlias $hostname" >> "$etcconf"
 1907                 echo "    DocumentRoot $docroot" >> "$etcconf"
 1908                 if [[ $httpproto == https ]]; then
 1909                     echo "    RewriteEngine On" >> "$etcconf"
 1910                     echo "    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)" >> "$etcconf"
 1911                     echo "    RewriteRule .* - [F]" >> "$etcconf"
 1912                     echo "    RewriteRule /management/other/ca.cert.der$ - [L]" >> "$etcconf"
 1913                     echo "    RewriteCond %{HTTPS} off" >> "$etcconf"
 1914                     echo "    RewriteRule (.*) https://%{HTTP_HOST}/\$1 [R,L]" >> "$etcconf"
 1915                     echo "</VirtualHost>" >> "$etcconf"
 1916                     echo "<VirtualHost *:443>" >> "$etcconf"
 1917                     echo "    KeepAlive Off" >> "$etcconf"
 1918                     echo "    <FilesMatch \"\.php\$\">" >> "$etcconf"
 1919                     if [[ $osid -eq 1 && $OSVersion -lt 7 ]]; then
 1920                         echo "        SetHandler application/x-httpd-php" >> "$etcconf"
 1921                     else
 1922                         echo "        SetHandler \"proxy:fcgi://127.0.0.1:9000/\"" >> "$etcconf"
 1923                     fi
 1924                     echo "    </FilesMatch>" >> "$etcconf"
 1925                     echo "    ServerName $ipaddress" >> "$etcconf"
 1926                     echo "    ServerAlias $hostname" >> "$etcconf"
 1927                     echo "    DocumentRoot $docroot" >> "$etcconf"
 1928                     echo "    SSLEngine On" >> "$etcconf"
 1929                     echo "    SSLProtocol all -SSLv3 -SSLv2" >> "$etcconf"
 1930                     echo "    SSLCipherSuite ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA" >> "$etcconf"
 1931                     echo "    SSLHonorCipherOrder On" >> "$etcconf"
 1932                     echo "    SSLCertificateFile $webdirdest/management/other/ssl/srvpublic.crt" >> "$etcconf"
 1933                     echo "    SSLCertificateKeyFile $sslprivkey" >> "$etcconf"
 1934                     echo "    SSLCACertificateFile $webdirdest/management/other/ca.cert.pem" >> "$etcconf"
 1935                     echo "    <Directory $webdirdest>" >> "$etcconf"
 1936                     echo "        DirectoryIndex index.php index.html index.htm" >> "$etcconf"
 1937                     echo "    </Directory>" >> "$etcconf"
 1938                     echo "    RewriteEngine On" >> "$etcconf"
 1939                     echo "    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)" >> "$etcconf"
 1940                     echo "    RewriteRule .* - [F]" >> "$etcconf"
 1941                     echo "    RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f" >> "$etcconf"
 1942                     echo "    RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d" >> "$etcconf"
 1943                     echo "    RewriteRule ^/fog/(.*)$ /fog/api/index.php [QSA,L]" >> "$etcconf"
 1944                     echo "</VirtualHost>" >> "$etcconf"
 1945                 else
 1946                     echo "    KeepAlive Off" >> "$etcconf"
 1947                     echo "    <Directory $webdirdest>" >> "$etcconf"
 1948                     echo "        DirectoryIndex index.php index.html index.htm" >> "$etcconf"
 1949                     echo "    </Directory>" >> "$etcconf"
 1950                     echo "    RewriteEngine On" >> "$etcconf"
 1951                     echo "    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)" >> "$etcconf"
 1952                     echo "    RewriteRule .* - [F]" >> "$etcconf"
 1953                     echo "    RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f" >> "$etcconf"
 1954                     echo "    RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d" >> "$etcconf"
 1955                     echo "    RewriteRule ^/fog/(.*)$ /fog/api/index.php [QSA,L]" >> "$etcconf"
 1956                     echo "</VirtualHost>" >> "$etcconf"
 1957                 fi
 1958                 diffconfig "${etcconf}"
 1959                 errorStat $?
 1960                 ln -s $webdirdest $webdirdest/ >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1961                 case $osid in
 1962                     1)
 1963                         phpfpmconf='/etc/php-fpm.d/www.conf';
 1964                         ;;
 1965                     2)
 1966                         if [[ $php_ver == 5 ]]; then
 1967                             phpfpmconf="/etc/php$php_ver/fpm/pool.d/www.conf"
 1968                         else
 1969                             phpfpmconf="/etc/php/$php_ver/fpm/pool.d/www.conf"
 1970                         fi
 1971                         ;;
 1972                     3)
 1973                         phpfpmconf='/etc/php/php-fpm.d/www.conf'
 1974                         ;;
 1975                 esac
 1976                 if [[ -n $phpfpmconf ]]; then
 1977                     sed -i 's/listen = .*/listen = 127.0.0.1:9000/g' $phpfpmconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1978                     sed -i 's/^[;]pm\.max_requests = .*/pm.max_requests = 2000/g' $phpfpmconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1979                     sed -i 's/^[;]php_admin_value\[memory_limit\] = .*/php_admin_value[memory_limit] = 256M/g' $phpfpmconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1980                     sed -i 's/pm\.max_children = .*/pm.max_children = 50/g' $phpfpmconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1981                     sed -i 's/pm\.min_spare_servers = .*/pm.min_spare_servers = 5/g' $phpfpmconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1982                     sed -i 's/pm\.max_spare_servers = .*/pm.max_spare_servers = 10/g' $phpfpmconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1983                     sed -i 's/pm\.start_servers = .*/pm.start_servers = 5/g' $phpfpmconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1984                 fi
 1985                 if [[ $osid -eq 2 ]]; then
 1986                     a2enmod $phpcmd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1987                     a2enmod proxy_fcgi setenvif >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1988                     a2enmod rewrite >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1989                     a2enmod ssl >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1990                     a2ensite "001-fog" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1991                     a2dissite "000-default" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 1992                 fi
 1993             ;;
 1994     esac
 1995     dots "Starting and checking status of web services"
 1996     case $systemctl in
 1997         yes)
 1998             case $osid in
 1999                 2)
 2000                     systemctl stop apache2 $phpfpm >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2001                     sleep 2
 2002                     systemctl start apache2 $phpfpm >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2003                     sleep 2
 2004                     systemctl status apache2 $phpfpm >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2005                     ;;
 2006                 *)
 2007                     systemctl stop httpd php-fpm >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2008                     sleep 2
 2009                     systemctl start httpd php-fpm >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2010                     sleep 2
 2011                     systemctl status httpd php-fpm >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2012                     ;;
 2013             esac
 2014             ;;
 2015         *)
 2016             case $osid in
 2017                 2)
 2018                     service apache2 stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2019                     sleep 2
 2020                     service apache2 start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2021                     sleep 2
 2022                     service $phpfpm stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2023                     sleep 2
 2024                     service $phpfpm start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2025                     sleep 2
 2026                     service apache2 status >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2027                     service $phpfpm status >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2028                     ;;
 2029                 *)
 2030                     service httpd stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2031                     sleep 2
 2032                     service httpd start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2033                     sleep 2
 2034                     service php-fpm stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2035                     sleep 2
 2036                     service php-fpm start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2037                     sleep 2
 2038                     service httpd status >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2039                     service php-fpm status >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2040                     ;;
 2041             esac
 2042             ;;
 2043     esac
 2044     errorStat $?
 2045     caCreated="yes"
 2046 }
 2047 configureHttpd() {
 2048     dots "Stopping web service"
 2049     case $systemctl in
 2050         yes)
 2051             case $osid in
 2052                 1|3)
 2053                     systemctl stop httpd php-fpm >>$workingdir/error_logs/fog_error_${version}.log 2>&1 && sleep 2
 2054                     ;;
 2055                 2)
 2056                     systemctl stop apache2 php${php_ver}-fpm >>$workingdir/error_logs/fog_error_${version}.log 2>&1 && sleep 2
 2057                     ;;
 2058             esac
 2059             errorStat $?
 2060             ;;
 2061         *)
 2062             case $osid in
 2063                 1)
 2064                     service httpd stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1 && sleep 2
 2065                     service php-fpm stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1 && sleep 2
 2066                     errorStat $?
 2067                     ;;
 2068                 2)
 2069                     service apache2 stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1 && sleep 2
 2070                     errorStat $?
 2071                     service php${php_ver}-fpm stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2072                     ;;
 2073             esac
 2074             ;;
 2075     esac
 2076     dots "Setting up Apache and PHP files"
 2077     if [[ ! -f $phpini ]]; then
 2078         echo "Failed"
 2079         echo "   ###########################################"
 2080         echo "   #                                         #"
 2081         echo "   #      PHP Failed to install properly     #"
 2082         echo "   #                                         #"
 2083         echo "   ###########################################"
 2084         echo
 2085         echo "   Could not find $phpini!"
 2086         exit 1
 2087     fi
 2088     if [[ $osid -eq 3 ]]; then
 2089         if [[ ! -f $httpdconf ]]; then
 2090             echo "   Apache configs not found!"
 2091             exit 1
 2092         fi
 2093         # Enable Event
 2094         sed -i '/LoadModule mpm_event_module modules\/mod_mpm_event.so/s/^#//g' $httpdconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2095         # Disable prefork and worker
 2096         sed -i '/LoadModule mpm_prefork_module modules\/mod_mpm_prefork.so/s/^/#/g' $httpdconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2097         sed -i '/LoadModule mpm_worker_module modules\/mod_mpm_worker.so/s/^/#/g' $httpdconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2098         # Enable proxy
 2099         sed -i '/LoadModule proxy_html_module modules\/mod_proxy_html.so/s/^#//g' $httpdconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2100         sed -i '/LoadModule xml2enc_module modules\/mod_xml2enc.so/s/^#//g' $httpdconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2101         sed -i '/LoadModule proxy_module modules\/mod_proxy.so/s/^#//g' $httpdconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2102         sed -i '/LoadModule proxy_http_module modules\/mod_proxy_http.so/s/^#//g' $httpdconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2103         sed -i '/LoadModule proxy_fcgi_module modules\/mod_proxy_fcgi.so/s/^#//g' $httpdconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2104         # Enable socache
 2105         sed -i '/LoadModule socache_shmcb_module modules\/mod_socache_shmcb.so/s/^#//g' $httpdconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2106         # Enable ssl
 2107         sed -i '/LoadModule ssl_module modules\/mod_ssl.so/s/^#//g' $httpdconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2108         # Enable rewrite
 2109         sed -i '/LoadModule rewrite_module modules\/mod_rewrite.so/s/^#//g' $httpdconf >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2110         # Enable our virtual host file for fog
 2111         grep -q "^Include conf/extra/fog\.conf" $httpdconf || echo -e "# FOG Virtual Host\nListen 443\nInclude conf/extra/fog.conf" >>$httpdconf
 2112         # Enable php extensions
 2113         sed -i 's/;extension=bcmath/extension=bcmath/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2114         sed -i 's/;extension=curl/extension=curl/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2115         sed -i 's/;extension=ftp/extension=ftp/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2116         sed -i 's/;extension=gd/extension=gd/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2117         sed -i 's/;extension=gettext/extension=gettext/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2118         sed -i 's/;extension=ldap/extension=ldap/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2119         sed -i 's/;extension=mysqli/extension=mysqli/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2120         sed -i 's/;extension=openssl/extension=openssl/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2121         sed -i 's/;extension=pdo_mysql/extension=pdo_mysql/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2122         sed -i 's/;extension=posix/extension=posix/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2123         sed -i 's/;extension=sockets/extension=sockets/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2124         sed -i 's/;extension=zip/extension=zip/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2125         sed -i 's/$open_basedir\ =/;open_basedir\ =/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2126     fi
 2127     sed -i 's/post_max_size\ \=\ 8M/post_max_size\ \=\ 3000M/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2128     sed -i 's/upload_max_filesize\ \=\ 2M/upload_max_filesize\ \=\ 3000M/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2129     sed -i 's/.*max_input_vars\ \=.*$/max_input_vars\ \=\ 250000/g' $phpini >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2130     errorStat $?
 2131     dots "Testing and removing symbolic links if found"
 2132     if [[ -h ${docroot}fog ]]; then
 2133         rm -f ${docroot}fog >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2134     fi
 2135     if [[ -h ${docroot}${webroot} ]]; then
 2136         rm -f ${docroot}${webroot} >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2137     fi
 2138     errorStat $?
 2139     dots "Backing up old data"
 2140     if [[ -d $backupPath/fog_web_${version}.BACKUP ]]; then
 2141         rm -rf $backupPath/fog_web_${version}.BACKUP >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2142     fi
 2143     if [[ -d $webdirdest ]]; then
 2144         cp -RT "$webdirdest" "${backupPath}/fog_web_${version}.BACKUP" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2145         rm -rf ${backupPath}/fog_web_${version}.BACKUP/lib/plugins/accesscontrol
 2146         rm -rf "$webdirdest" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2147     fi
 2148     if [[ $osid -eq 2 ]]; then
 2149         if [[ -d ${docroot}fog ]]; then
 2150             rm -rf ${docroot} >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2151         fi
 2152     fi
 2153     mkdir -p "$webdirdest" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2154     if [[ -d $docroot && ! -h ${docroot}fog ]] || [[ ! -d ${docroot}fog ]]; then
 2155         ln -s $webdirdest  ${docroot}/fog >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2156     fi
 2157     errorStat $?
 2158     if [[ $copybackold -gt 0 ]]; then
 2159         if [[ -d ${backupPath}/fog_web_${version}.BACKUP ]]; then
 2160             dots "Copying back old web folder as is";
 2161             cp -Rf ${backupPath}/fog_web_${version}.BACKUP/* $webdirdest/
 2162             errorStat $?
 2163             dots "Ensuring all classes are lowercased"
 2164             for i in $(find $webdirdest -type f -name "*[A-Z]*\.class\.php" -o -name "*[A-Z]*\.event\.php" -o -name "*[A-Z]*\.hook\.php" 2>>$workingdir/error_logs/fog_error_${version}.log); do
 2165                 mv "$i" "$(echo $i | tr A-Z a-z)" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2166             done
 2167             errorStat $?
 2168         fi
 2169     fi
 2170     dots "Copying new files to web folder"
 2171     cp -Rf $webdirsrc/* $webdirdest/
 2172     errorStat $?
 2173     for i in $(find $backupPath/fog_web_${version}.BACKUP/management/other/ -maxdepth 1 -type f -not -name gpl-3.0.txt -a -not -name index.php -a -not -name 'ca.*' 2>>$workingdir/error_logs/fog_error_${version}.log); do
 2174         cp -Rf $i ${webdirdest}/management/other/ >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2175     done
 2176     if [[ $installlang -eq 1 ]]; then
 2177         dots "Creating the language binaries"
 2178         langpath="${webdirdest}/management/languages"
 2179         languagesfound=$(find $langpath -maxdepth 1 -type d -exec basename {} \; | awk -F. '/\./ {print $1}' 2>>$workingdir/error_logs/fog_error_${version}.log)
 2180         languagemogen "$languagesfound" "$langpath"
 2181         echo "Done"
 2182     fi
 2183     dots "Creating config file"
 2184     phpescsnmysqlpass="${snmysqlpass//\\/\\\\}";   # Replace every \ with \\ ...
 2185     phpescsnmysqlpass="${phpescsnmysqlpass//\'/\\\'}"   # and then every ' with \' for full PHP escaping
 2186     echo "<?php
 2187 /**
 2188  * The main configuration FOG uses.
 2189  *
 2190  * PHP Version 5
 2191  *
 2192  * Constructs the configuration we need to run FOG.
 2193  *
 2194  * @category Config
 2195  * @package  FOGProject
 2196  * @author   Tom Elliott <tommygunsster@gmail.com>
 2197  * @license  http://opensource.org/licenses/gpl-3.0 GPLv3
 2198  * @link     https://fogproject.org
 2199  */
 2200 /**
 2201  * The main configuration FOG uses.
 2202  *
 2203  * @category Config
 2204  * @package  FOGProject
 2205  * @author   Tom Elliott <tommygunsster@gmail.com>
 2206  * @license  http://opensource.org/licenses/gpl-3.0 GPLv3
 2207  * @link     https://fogproject.org
 2208  */
 2209 class Config
 2210 {
 2211     /**
 2212      * Calls the required functions to define items
 2213      *
 2214      * @return void
 2215      */
 2216     public function __construct()
 2217     {
 2218         global \$node;
 2219         self::_dbSettings();
 2220         self::_svcSetting();
 2221         if (\$node == 'schema') {
 2222             self::_initSetting();
 2223         }
 2224     }
 2225     /**
 2226      * Defines the database settings for FOG
 2227      *
 2228      * @return void
 2229      */
 2230     private static function _dbSettings()
 2231     {
 2232         define('DATABASE_TYPE', 'mysql'); // mysql or oracle
 2233         define('DATABASE_HOST', '$snmysqlhost');
 2234         define('DATABASE_NAME', '$mysqldbname');
 2235         define('DATABASE_USERNAME', '$snmysqluser');
 2236         define('DATABASE_PASSWORD', '$phpescsnmysqlpass');
 2237     }
 2238     /**
 2239      * Defines the service settings
 2240      *
 2241      * @return void
 2242      */
 2243     private static function _svcSetting()
 2244     {
 2245         define('UDPSENDERPATH', '/usr/local/sbin/udp-sender');
 2246         define('MULTICASTINTERFACE', '${interface}');
 2247         define('UDPSENDER_MAXWAIT', null);
 2248     }
 2249     /**
 2250      * Initial values if fresh install are set here
 2251      * NOTE: These values are only used on initial
 2252      * installation to set the database values.
 2253      * If this is an upgrade, they do not change
 2254      * the values within the Database.
 2255      * Please use FOG Configuration->FOG Settings
 2256      * to change these values after everything is
 2257      * setup.
 2258      *
 2259      * @return void
 2260      */
 2261     private static function _initSetting()
 2262     {
 2263         define('TFTP_HOST', \"${ipaddress}\");
 2264         define('TFTP_FTP_USERNAME', \"${username}\");
 2265         define(
 2266             'TFTP_FTP_PASSWORD',
 2267             \"${password}\"
 2268         );
 2269         define('TFTP_PXE_KERNEL_DIR', \"${webdirdest}/service/ipxe/\");
 2270         define('PXE_KERNEL', 'bzImage');
 2271         define('PXE_KERNEL_RAMDISK', 275000);
 2272         define('USE_SLOPPY_NAME_LOOKUPS', true);
 2273         define('MEMTEST_KERNEL', 'memtest.bin');
 2274         define('PXE_IMAGE', 'init.xz');
 2275         define('STORAGE_HOST', \"${ipaddress}\");
 2276         define('STORAGE_FTP_USERNAME', \"${username}\");
 2277         define(
 2278             'STORAGE_FTP_PASSWORD',
 2279             \"${password}\"
 2280         );
 2281         define('STORAGE_DATADIR', '${storageLocation}/');
 2282         define('STORAGE_DATADIR_CAPTURE', '${storageLocationCapture}');
 2283         define('STORAGE_BANDWIDTHPATH', '${webroot}status/bandwidth.php');
 2284         define('STORAGE_INTERFACE', '${interface}');
 2285         define('CAPTURERESIZEPCT', 5);
 2286         define('WEB_HOST', \"${ipaddress}\");
 2287         define('WOL_HOST', \"${ipaddress}\");
 2288         define('WOL_PATH', '/${webroot}wol/wol.php');
 2289         define('WOL_INTERFACE', \"${interface}\");
 2290         define('SNAPINDIR', \"${snapindir}/\");
 2291         define('QUEUESIZE', '10');
 2292         define('CHECKIN_TIMEOUT', 600);
 2293         define('USER_MINPASSLENGTH', 4);
 2294         define('NFS_ETH_MONITOR', \"${interface}\");
 2295         define('UDPCAST_INTERFACE', \"${interface}\");
 2296         // Must be an even number! recommended between 49152 to 65535
 2297         define('UDPCAST_STARTINGPORT', 63100);
 2298         define('FOG_MULTICAST_MAX_SESSIONS', 64);
 2299         define('FOG_JPGRAPH_VERSION', '2.3');
 2300         define('FOG_REPORT_DIR', './reports/');
 2301         define('FOG_CAPTUREIGNOREPAGEHIBER', true);
 2302         define('FOG_THEME', 'default/fog.css');
 2303     }
 2304 }" > "${webdirdest}/lib/fog/config.class.php"
 2305     errorStat $?
 2306     dots "Creating redirection index file"
 2307     if [[ ! -f ${docroot}/index.php ]]; then
 2308         echo "<?php
 2309 header('Location: /fog/index.php');
 2310 die();
 2311 ?>" > ${docroot}/index.php && chown ${apacheuser}:${apacheuser} ${docroot}/index.php
 2312         errorStat $?
 2313     else
 2314         echo "Skipped"
 2315     fi
 2316     downloadfiles
 2317     if [[ $osid -eq 2 ]]; then
 2318         php -m | grep mysqlnd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2319         if [[ ! $? -eq 0 ]]; then
 2320             ${phpcmd}enmod mysqlnd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2321             if [[ ! $? -eq 0 ]]; then
 2322                 if [[ -e /etc/php${php_ver}/conf.d/mysqlnd.ini ]]; then
 2323                     cp -f "/etc/php${php_ver}/conf.d/mysqlnd.ini" "/etc/php${php_ver}/mods-available/php${php_ver}-mysqlnd.ini" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2324                     ${phpcmd}enmod mysqlnd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2325                 fi
 2326             fi
 2327         fi
 2328     fi
 2329     dots "Enabling apache and fpm services on boot"
 2330     if [[ $osid -eq 2 ]]; then
 2331         if [[ $systemctl == yes ]]; then
 2332             systemctl enable apache2 >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2333             systemctl enable $phpfpm >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2334         else
 2335             sysv-rc-conf apache2 on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2336             sysv-rc-conf $phpfpm on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2337         fi
 2338     elif [[ $systemctl == yes ]]; then
 2339         systemctl enable httpd php-fpm >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2340     else
 2341         chkconfig php-fpm on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2342         chkconfig httpd on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2343     fi
 2344     errorStat $?
 2345     createSSLCA
 2346     dots "Changing permissions on apache log files"
 2347     chmod +rx $apachelogdir
 2348     chmod +rx $apacheerrlog
 2349     chmod +rx $apacheacclog
 2350     chown -R ${apacheuser}:${apacheuser} $webdirdest
 2351     errorStat $?
 2352     [[ -d /var/www/html/ && ! -e /var/www/html/fog/ ]] && ln -s "$webdirdest" /var/www/html/
 2353     [[ -d /var/www/ && ! -e /var/www/fog ]] && ln -s "$webdirdest" /var/www/
 2354     chown -R ${apacheuser}:${apacheuser} "$webdirdest"
 2355     chown -R ${username}:${apacheuser} "$webdirdest/service/ipxe"
 2356 }
 2357 downloadfiles() {
 2358     dots "Downloading kernel, init and fog-client binaries"
 2359     clientVer="$(awk -F\' /"define\('FOG_CLIENT_VERSION'[,](.*)"/'{print $4}' ../packages/web/lib/fog/system.class.php | tr -d '[[:space:]]')"
 2360     fosURL="https://github.com/FOGProject/fos/releases/download"
 2361     fogclientURL="https://github.com/FOGProject/fog-client/releases/download"
 2362     [[ ! -d ../tmp/ ]] && mkdir -p ../tmp/ >/dev/null 2>&1
 2363     cwd=$(pwd)
 2364     cd ../tmp/
 2365     if [[ $version =~ ^[0-9]\.[0-9]\.[0-9]$ ]]
 2366     then
 2367         urls=( "${fosURL}/${version}/init.xz" "${fosURL}/${version}/init_32.xz" "${fosURL}/${version}/bzImage" "${fosURL}/${version}/bzImage32" "${fogclientURL}/${clientVer}/FOGService.msi" "${fogclientURL}/${clientVer}/SmartInstaller.exe" )
 2368         if [[ $armsupport == 1 ]]; then
 2369             urls+=( "${fosURL}/${version}/arm_init.cpio.gz" "${fosURL}/${version}/arm_Image" )
 2370         fi
 2371     else
 2372         urls=( "https://fogproject.org/inits/init.xz" "https://fogproject.org/inits/init_32.xz" "https://fogproject.org/kernels/bzImage" "https://fogproject.org/kernels/bzImage32" "${fogclientURL}/${clientVer}/FOGService.msi" "${fogclientURL}/${clientVer}/SmartInstaller.exe" )
 2373         if [[ $armsupport == 1 ]]; then
 2374             urls+=( "https://fogproject.org/inits/arm_init.cpio.gz" "https://fogproject.org/kernels/arm_Image" )
 2375         fi
 2376     fi
 2377     for url in "${urls[@]}"
 2378     do
 2379         checksum=1
 2380         cnt=0
 2381         filename=$(basename -- "$url")
 2382         hashfile="${filename}.sha256"
 2383         baseurl=$(dirname -- "$url")
 2384         hashurl="${baseurl}/${hashfile}"
 2385         # make sure we download the most recent hash file to start with
 2386         if [[ -f $hashfile ]]; then
 2387             rm -f $hashfile
 2388             curl --silent -kOL $hashurl >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2389         fi
 2390         while [[ $checksum -ne 0 && $cnt -lt 10 ]]
 2391         do
 2392             [[ -f $hashfile ]] && sha256sum --check $hashfile >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2393             checksum=$?
 2394             if [[ $checksum -ne 0 ]]
 2395             then
 2396                 curl --silent -kOL $url >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2397                 curl --silent -kOL $hashurl >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2398             fi
 2399             let cnt+=1
 2400         done
 2401         if [[ $checksum -ne 0 ]]
 2402         then
 2403             echo " * Could not download $filename properly"
 2404             [[ -z $exitFail ]] && exit 1
 2405         fi
 2406     done
 2407     echo "Done"
 2408     dots "Copying binaries to destination paths"
 2409     cp -vf ${copypath}bzImage ${webdirdest}/service/ipxe/ >>$workingdir/error_logs/fog_error_${version}.log 2>&1 || errorStat $?
 2410     cp -vf ${copypath}bzImage32 ${webdirdest}/service/ipxe/ >>$workingdir/error_logs/fog_error_${version}.log 2>&1 || errorStat $?
 2411     cp -vf ${copypath}init.xz ${webdirdest}/service/ipxe/ >>$workingdir/error_logs/fog_error_${version}.log 2>&1 || errorStat $?
 2412     cp -vf ${copypath}init_32.xz ${webdirdest}/service/ipxe/ >>$workingdir/error_logs/fog_error_${version}.log 2>&1 || errorStat $?
 2413     if [[ $armsupport == 1 ]]; then
 2414         cp -vf ${copypath_arm}arm_Image ${webdirdest}/service/ipxe/ >>$workingdir/error_logs/fog_error_${version}.log 2>&1 || errorStat $?
 2415         cp -vf ${copypath_arm}arm_init.cpio.gz ${webdirdest}/service/ipxe/ >>$workingdir/error_logs/fog_error_${version}.log 2>&1 || errorStat $?
 2416     fi
 2417     cp -vf ${copypath}FOGService.msi ${copypath}SmartInstaller.exe ${webdirdest}/client/ >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2418     errorStat $?
 2419     cd $cwd
 2420 }
 2421 configureDHCP() {
 2422     case $linuxReleaseName in
 2423         *[Dd][Ee][Bb][Ii][Aa][Nn]*)
 2424             if [[ $bldhcp -eq 1 ]]; then
 2425                 dots "Setting up and starting DHCP Server (incl. debian 9 fix)"
 2426                 sed -i.fog "s/INTERFACESv4=\"\"/INTERFACESv4=\"$interface\"/g" /etc/default/isc-dhcp-server
 2427             else
 2428                 dots "Setting up and starting DHCP Server"
 2429             fi
 2430             ;;
 2431         *)
 2432             dots "Setting up and starting DHCP Server"
 2433             ;;
 2434     esac
 2435     case $bldhcp in
 2436         1)
 2437             serverip=$(ip -4 -o addr show $interface | awk -F'([ /])+' '/global/ {print $4}')
 2438             [[ -z $serverip ]] && serverip=$(/sbin/ifconfig $interface | grep -oE 'inet[:]? addr[:]?([0-9]{1,3}\.){3}[0-9]{1,3}' | awk -F'(inet[:]? ?addr[:]?)' '{print $2}')
 2439             [[ -z $submask ]] && submask=$(cidr2mask $(getCidr $interface))
 2440             network=$(mask2network $serverip $submask)
 2441             [[ -z $startrange ]] && startrange=$(addToAddress $network 10)
 2442             [[ -z $endrange ]] && endrange=$(subtract1fromAddress $(echo $(interface2broadcast $interface)))
 2443             [[ -f $dhcpconfig ]] && dhcptouse=$dhcpconfig
 2444             [[ -f $dhcpconfigother ]] && dhcptouse=$dhcpconfigother
 2445             if [[ -z $dhcptouse || ! -f $dhcptouse ]]; then
 2446                 echo "Failed"
 2447                 echo "Could not find dhcp config file"
 2448                 exit 1
 2449             fi
 2450             mv -fv "${dhcptouse}" "${dhcptouse}.${timestamp}" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2451             [[ -z $bootfilename ]] && bootfilename="undionly.kpxe"
 2452             echo "# DHCP Server Configuration file\n#see /usr/share/doc/dhcp*/dhcpd.conf.sample" > $dhcptouse
 2453             echo "# This file was created by FOG" >> "$dhcptouse"
 2454             echo "#Definition of PXE-specific options" >> "$dhcptouse"
 2455             echo "# Code 1: Multicast IP Address of bootfile" >> "$dhcptouse"
 2456             echo "# Code 2: UDP Port that client should monitor for MTFTP Responses" >> "$dhcptouse"
 2457             echo "# Code 3: UDP Port that MTFTP servers are using to listen for MTFTP requests" >> "$dhcptouse"
 2458             echo "# Code 4: Number of seconds a client must listen for activity before trying" >> "$dhcptouse"
 2459             echo "#         to start a new MTFTP transfer" >> "$dhcptouse"
 2460             echo "# Code 5: Number of seconds a client must listen before trying to restart" >> "$dhcptouse"
 2461             echo "#         a MTFTP transfer" >> "$dhcptouse"
 2462             echo "option space PXE;" >> "$dhcptouse"
 2463             echo "option PXE.mtftp-ip code 1 = ip-address;" >> "$dhcptouse"
 2464             echo "option PXE.mtftp-cport code 2 = unsigned integer 16;" >> "$dhcptouse"
 2465             echo "option PXE.mtftp-sport code 3 = unsigned integer 16;" >> "$dhcptouse"
 2466             echo "option PXE.mtftp-tmout code 4 = unsigned integer 8;" >> "$dhcptouse"
 2467             echo "option PXE.mtftp-delay code 5 = unsigned integer 8;" >> "$dhcptouse"
 2468             echo "option arch code 93 = unsigned integer 16;" >> "$dhcptouse"
 2469             echo "use-host-decl-names on;" >> "$dhcptouse"
 2470             echo "ddns-update-style interim;" >> "$dhcptouse"
 2471             echo "ignore client-updates;" >> "$dhcptouse"
 2472             echo "# Specify subnet of ether device you do NOT want service." >> "$dhcptouse"
 2473             echo "# For systems with two or more ethernet devices." >> "$dhcptouse"
 2474             echo "# subnet 136.165.0.0 netmask 255.255.0.0 {}" >> "$dhcptouse"
 2475             echo "subnet $network netmask $submask{" >> "$dhcptouse"
 2476             echo "    option subnet-mask $submask;" >> "$dhcptouse"
 2477             echo "    range dynamic-bootp $startrange $endrange;" >> "$dhcptouse"
 2478             echo "    default-lease-time 21600;" >> "$dhcptouse"
 2479             echo "    max-lease-time 43200;" >> "$dhcptouse"
 2480             [[ ! $(validip $routeraddress) -eq 0 ]] && routeraddress=$(echo $routeraddress | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
 2481             [[ ! $(validip $dnsaddress) -eq 0 ]] && dnsaddress=$(echo $dnsaddress | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
 2482             [[ $(validip $routeraddress) -eq 0 ]] && echo "    option routers $routeraddress;" >> "$dhcptouse" || ( echo "    #option routers 0.0.0.0" >> "$dhcptouse" && echo " !!! No router address found !!!" )
 2483             [[ $(validip $dnsaddress) -eq 0 ]] && echo "    option domain-name-servers $dnsaddress;" >> "$dhcptouse" || ( echo "    #option routers 0.0.0.0" >> "$dhcptouse" && echo " !!! No dns address found !!!" )
 2484             echo "    next-server $ipaddress;" >> "$dhcptouse"
 2485             echo "    class \"Legacy\" {" >> "$dhcptouse"
 2486             echo "        match if substring(option vendor-class-identifier, 0, 20) = \"PXEClient:Arch:00000\";" >> "$dhcptouse"
 2487             echo "        filename \"undionly.kkpxe\";" >> "$dhcptouse"
 2488             echo "    }" >> "$dhcptouse"
 2489             echo "    class \"UEFI-32-2\" {" >> "$dhcptouse"
 2490             echo "        match if substring(option vendor-class-identifier, 0, 20) = \"PXEClient:Arch:00002\";" >> "$dhcptouse"
 2491             echo "        filename \"i386-efi/ipxe.efi\";" >> "$dhcptouse"
 2492             echo "    }" >> "$dhcptouse"
 2493             echo "    class \"UEFI-32-1\" {" >> "$dhcptouse"
 2494             echo "        match if substring(option vendor-class-identifier, 0, 20) = \"PXEClient:Arch:00006\";" >> "$dhcptouse"
 2495             echo "        filename \"i386-efi/ipxe.efi\";" >> "$dhcptouse"
 2496             echo "    }" >> "$dhcptouse"
 2497             echo "    class \"UEFI-64-1\" {" >> "$dhcptouse"
 2498             echo "        match if substring(option vendor-class-identifier, 0, 20) = \"PXEClient:Arch:00007\";" >> "$dhcptouse"
 2499             echo "        filename \"ipxe.efi\";" >> "$dhcptouse"
 2500             echo "    }" >> "$dhcptouse"
 2501             echo "    class \"UEFI-64-2\" {" >> "$dhcptouse"
 2502             echo "        match if substring(option vendor-class-identifier, 0, 20) = \"PXEClient:Arch:00008\";" >> "$dhcptouse"
 2503             echo "        filename \"ipxe.efi\";" >> "$dhcptouse"
 2504             echo "    }" >> "$dhcptouse"
 2505             echo "    class \"UEFI-64-3\" {" >> "$dhcptouse"
 2506             echo "        match if substring(option vendor-class-identifier, 0, 20) = \"PXEClient:Arch:00009\";" >> "$dhcptouse"
 2507             echo "        filename \"ipxe.efi\";" >> "$dhcptouse"
 2508             echo "    }" >> "$dhcptouse"
 2509             echo "    class \"SURFACE-PRO-4\" {" >> "$dhcptouse"
 2510             echo "        match if substring(option vendor-class-identifier, 0, 32) = \"PXEClient:Arch:00007:UNDI:003016\";" >> "$dhcptouse"
 2511             echo "        filename \"ipxe.efi\";" >> "$dhcptouse"
 2512             echo "    }" >> "$dhcptouse"
 2513             echo "    class \"Apple-Intel-Netboot\" {" >> "$dhcptouse"
 2514             echo "        match if substring(option vendor-class-identifier, 0, 14) = \"AAPLBSDPC/i386\";" >> "$dhcptouse"
 2515             echo "        option dhcp-parameter-request-list 1,3,17,43,60;" >> "$dhcptouse"
 2516             echo "        if (option dhcp-message-type = 8) {" >> "$dhcptouse"
 2517             echo "            option vendor-class-identifier \"AAPLBSDPC\";" >> "$dhcptouse"
 2518             echo "            if (substring(option vendor-encapsulated-options, 0, 3) = 01:01:01) {" >> "$dhcptouse"
 2519             echo "                # BSDP List" >> "$dhcptouse"
 2520             echo "                option vendor-encapsulated-options 01:01:01:04:02:80:00:07:04:81:00:05:2a:09:0D:81:00:05:2a:08:69:50:58:45:2d:46:4f:47;" >> "$dhcptouse"
 2521             echo "                filename \"ipxe.efi\";" >> "$dhcptouse"
 2522             echo "            }" >> "$dhcptouse"
 2523             echo "        }" >> "$dhcptouse"
 2524             echo "    }" >> "$dhcptouse"
 2525             echo "}" >> "$dhcptouse"
 2526             diffconfig "${dhcptouse}"
 2527             case $systemctl in
 2528                 yes)
 2529                     systemctl enable $dhcpd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2530                     systemctl stop $dhcpd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2531                     sleep 2
 2532                     systemctl start $dhcpd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2533                     sleep 2
 2534                     systemctl status $dhcpd >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2535                     ;;
 2536                 *)
 2537                     case $osid in
 2538                         1)
 2539                             chkconfig $dhcpd on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2540                             service $dhcpd stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2541                             sleep 2
 2542                             service $dhcpd start >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2543                             sleep 2
 2544                             service $dhcpd status >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2545                             ;;
 2546                         2)
 2547                             sysv-rc-conf $dhcpd on >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2548                             /etc/init.d/$dhcpd stop >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2549                             sleep 2
 2550                             /etc/init.d/$dhcpd start >>$workingdir/error_logs/fog_error_${version}.log 2>&1 && sleep 2
 2551                             ;;
 2552                     esac
 2553                     ;;
 2554             esac
 2555             errorStat $?
 2556             ;;
 2557         *)
 2558             echo "Skipped"
 2559             ;;
 2560     esac
 2561 }
 2562 vercomp() {
 2563     [[ $1 == $2 ]] && return 0
 2564     local IFS=.
 2565     local i ver1=($1) ver2=($2)
 2566     for ((i=${#ver1[@]}; i<${#ver2}; i++)); do
 2567         ver1[i]=0
 2568     done
 2569     for ((i=0; i<${#ver1[@]}; i++)); do
 2570         [[ -z ${ver2[i]} ]] && ver2[i]=0
 2571         if ((10#${ver1[i]} > 10#${ver2[i]})); then
 2572             return 1
 2573         fi
 2574         if ((10#${ver1[i]} < 10#${ver2[i]})); then
 2575             return 2
 2576         fi
 2577     done
 2578     return 0
 2579 }
 2580 languagemogen() {
 2581     local languages="$1"
 2582     local langpath="$2"
 2583     local IFS=$'\n'
 2584     local lang=''
 2585     for lang in ${languages[@]}; do
 2586         [[ ! -d "${langpath}/${lang}.UTF-8" ]] && continue
 2587         msgfmt -o \
 2588             "${langpath}/${lang}.UTF-8/LC_MESSAGES/messages.mo" \
 2589             "${langpath}/${lang}.UTF-8/LC_MESSAGES/messages.po" \
 2590             >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2591     done
 2592 }
 2593 generatePassword() {
 2594     local length="$1"
 2595     [[ $length -ge 12 && $length -le 128 ]] || length=20
 2596 
 2597     while [[ ${#genpassword} -lt $((length-1)) || -z $special ]]; do
 2598         newchar=$(head -c1 /dev/urandom | tr -dc '0-9a-zA-Z!#$%&()*+,-./:;<=>?@[]^_{|}~')
 2599         if [[ -n $(echo $newchar | tr -dc '!#$%&()*+,-./:;<=>?@[]^_{|}~') ]]; then
 2600             special=${newchar}
 2601         elif [[ ${#genpassword} -lt $((length-1)) ]]; then
 2602             genpassword=${genpassword}${newchar}
 2603         fi
 2604     done
 2605     # 9$(date +%N) seems weird but it's important because date may return
 2606     # a leading 0 causing modulo to fail on reading it as octal number
 2607     position=$(( 9$(date +%N) % $length ))
 2608     # inject the special character at a random position
 2609     echo ${genpassword::($position)}$special${genpassword:($position)}
 2610 }
 2611 checkPasswordChars() {
 2612     echo "$i" | tr -d '0-9a-zA-Z!#$%&()*+,-./:;<=>?@[]^_{|}~'
 2613 }
 2614 diffconfig() {
 2615     local conffile="$1"
 2616     [[ ! -f "${conffile}.${timestamp}" ]] && return 0
 2617     diff -q "${conffile}" "${conffile}.${timestamp}" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2618     if [[ $? -eq 0 ]]; then
 2619         rm -f "${conffile}.${timestamp}" >>$workingdir/error_logs/fog_error_${version}.log 2>&1
 2620     else
 2621         backupconfig="${backupconfig} ${conffile}"
 2622     fi
 2623 }