apache22to24

#!/bin/bash
# WF 2015-11-28
# check htaccess files to be upgraded
# from Apache 2.2 to 2.4

#
# fix
#
fix() {
  local l_htaccess="$1"
  local l_newhtaccess=/tmp/htaccess$$
  local l_diff=/tmp/htaccessdiff$$
  cat $l_htaccess | awk '
/Order/ { 
  next
}
/Allow from all/ { 
   print "Require all granted"
   next
}
/Deny from all/ { 
   print "Require all denied"
   next 
}
{ print }
' > $l_newhtaccess 
  diff $l_htaccess $l_newhtaccess > $l_diff
  if [ $? -ne 0 ]
  then
    echo $l_htaccess had been modified
    tail -8 $l_htaccess
    tail -8 $l_newhtaccess
    tail -8 $l_diff 
    echo -n "shall the new configuration be used y/n/c? "
    read answer
    #dialog --backtitle "shall the new configuration be used? " \
    #       --begin 2 2 --title $l_htaccess \
    #       --textbox $l_htaccess 8 70 
    #       --textbox $l_newhtaccess 8 70 
    #       --textbox $l_diff 8 70 
    case $answer in
      y) 
        mv "$l_newhtaccess" "$l_htaccess"
      ;;
      n) 
       rm $l_newhtaccess
       rm $l_diff
       ;;
     c)  
       rm $l_newhtaccess
       rm $l_diff
       exit 1
       ;;
    esac 
  else
    echo $l_htaccess is unchanged 
    cat $l_htaccess
  fi
  rm $l_newhtaccess
  rm $l_diff
}

#
# loop over the htaccess files
#
docheck() {
  local l_htaccesslist="$1"
  local l_wwwroot="$2"
  for file in `cat $l_htaccesslist`
  do
    htaccess=$l_wwwroot/$file
    needsfix=false
    for keywords in Order Deny Allow
    do
      grep -i deny $htaccess > /dev/null
      if [ $? -eq 0 ]
      then
        needsfix=true 
      fi
    done
    if [ "$needsfix" = "true" ]
    then
      fix $htaccess
    fi
  done
}

htaccesslist=/tmp/htaccess.txt
wwwroot=/var/www
cd $wwwroot 
find . -name .htaccess > $htaccesslist
docheck $htaccesslist $wwwroot