export HISTTIMEFORMAT="%Y-%m-%d %T "
export OCTO_HISTORY_FILE="/root/.octo_history"
###############################
# Support helper-script by mr #
###############################
# regular colors
RST="\033[0;0m"    # RESET colors
K="\033[0;30m"    # black
R="\033[0;31m"    # red
G="\033[0;32m"    # green
Y="\033[0;33m"    # yellow
B="\033[0;34m"    # blue
M="\033[0;35m"    # magenta
C="\033[0;36m"    # cyan
W="\033[0;37m"    # white

# bold colors (same in bold)
EMK="\033[1;30m"  # black
EMR="\033[1;31m"  # red
EMG="\033[1;32m"  # green
EMY="\033[1;33m"  # yellow
EMB="\033[1;34m"  # blue
EMM="\033[1;35m"  # magenta
EMC="\033[1;36m"  # cyan
EMW="\033[1;37m"  # white

ST=80

##################################
#        CUSTOM FUNCTIONS        #
##################################

#################################
# Show colored title (title "<text>" "<fillerchar>")
title()
{
  SPACE=$((${#1}+2))

  printf "${C}"
  for (( i = 0; i < $ST; i++ )); do
    if [[ $i -eq $(($ST / 2 - $SPACE / 2)) ]]; then
      printf " $1 "
      i=$(($i+$SPACE))
    else
      printf "$2"
    fi
  done
  printf "${W}\n"
}

####################
# Initial sources! #
# Do not move!     #
####################
clear
title "OG Infos" "#"
source /etc/profile
source /root/.profile

#################################
# Show aliases and functions with definitions
mrhelp()
{
  echo ""
  title "ALIASES" "-"
  cat /tmp/.bashrc_temp | egrep "^alias [A-Za-z_0-9]+\=" | sed -e 's/alias //' | sed -e 's/\=/\t/' | sort | awk '{printf("%-22s %s \n",$1,$(0))}' | sed -r 's/\S+/     /1'
  title "FUNCTIONS" "-"
  cat /tmp/.bashrc_temp | grep -B 1 '.*()$' | sed -e '/--/d' -e 'N;s/\(.*\)\n\(.*\)/\2\t\1/;P;D' | grep -v "^ " | sort | awk '{printf("%-22s %s \n",$1,$(0))}' | sed -r 's/\S+/     /1'
  # cat /tmp/.bashrc_temp | grep -B 1 '.*()$' | sed -e '/--/d' -e 'N;s/\(.*\)\n\(.*\)/\2\t\1/;P;D' | sort | awk '{printf("%-22s %s \n",$1,$(0))}' | sed -r 's/\S+/     /1'
  title "END" "-"
  echo ""
}

#################################
# Prüfe auf überschüssige Whitelisteinträge
captPortCheck()
{
searcharray=(
        '(\*.apple.\*'
        '\*.gstatic/generate_204.\*'
        '\*.google/generate_204.\*'
        '\*.msftncsi/ncsi.\*'
        '\*.microsoftconnecttest/connecttest.\*'
        '\*.dns.\*'
        '\*.msftncsi.\*'
        '\*.microsoftconnecttest.\*'
        '\*.google.\*'
        '\*.gstatic.\*'
        '\*.ipv6.\*'
        '\*.clients3.\*'
        '\*.gsp3.\*'
        '\*.akamaitechnologies.\*'
        '\*.appleiphonecell.\*'
        '\*.airport.\*'
        '\*.apple.com.\*'
        '\*.akamaiedge.\*'
        '\*.captive.\*)'
)
mwaufruf=$(mysql webgui -e "select * from Whitelist")
cpckregex=$(echo ${searcharray[*]} | xargs echo -en | tr -s ' ' '|')
search=$(echo -e "$mwaufruf" | egrep $cpckregex | awk '{print $4,$2}'| column -t | \
	 grep -o 3.* | awk '{print $2}' | xargs echo -en | tr -s ' ' '|')
treffer=$(echo -e "$mwaufruf" | egrep $cpckregex | column -t | \
	  awk '{print $4}' | grep -i 3)
if [[ ! -z "$treffer" ]]; then
  title "Captive Portal Whitelist Check : gefährdende Webseitentreffer" "-"
  echo -e '\033[41m' $search '\033[0m' | sed "s/.\{79\}/&\\n/g"
fi;
}

#################################
# Execute sql-Statement on webgui
wgui()
{
  if [[ -z $1 ]]; then
    echo -e "\n${EMB}Usage:${W}\t\t wgui ${B}\"<sql-statement>\""
    echo -e "${EMB}${W}\t\t wgui ${B}\"<sql-statement>\" \"<table>\""
    echo -e "${EMC}Example:${W}\t wgui ${C}\"SELECT * FROM Users\""
    echo -e "${EMC}${W}\t\t wgui ${C}\"SHOW DATABASES\" \"\""
    echo -e "${EMC}${W}\t\t (empty <table>-param: shows all databases)"
    echo -e "${EMY}Note:${W}\t\t You ${R}don't${W} need the ${EMW}\";\"${W} at the end of the line"
    echo -e "${B}----------------------------------------------------------------\n"
  else
    if [[ -z $2 ]]; then
      mysql webgui -e "$1;"
    fi
    if [[ ! -z $2 ]]; then
      mysql "$2" -e "$1;"
    fi

  fi
}

#################################
# Add Mixed Mode user
add_mm_user()
{
    if [[ $# -eq 0 ]]; then
        echo -e "\n${EMB}Adds a mixed-mode User to the mysql Databse with Profile 5 (default)"
        title "" "-"
        echo -e "${EMB}Usage:${W}\t\t\t ${FUNCNAME[0]} ${B}\"<username> <subnet> <subnetmask in BIT>\""
        echo -e "${W}or"
        echo -e "${EMB}Usage:${W}\t\t\t ${FUNCNAME[0]} ${B}\"<ip> <subnetmask in BIT>\""
        title "" "-"
        echo -e "${EMC}Example 2408 (SEG):${W}\t add_mm_user ${C}mm_seg 34 20 ${W}\t (1)"
        echo -e "${EMC}Example 2409 (LEHRER):${W}\t add_mm_user ${C}mm_lehrer 48 20 ${W}\t (2)"
        echo -e "${EMC}Example 2410 (GAESTE):${W}\t add_mm_user ${C}mm_gast 64 20 ${W}\t (3)"
        echo -e "${EMC}Example ip address:${W}\t add_mm_user ${C}10.1.1.1 32 ${W}\t (4) ${Y}You will be ASKED for IP and SUBNETMASK(BIT)!"
        echo -e "${W}You can also choose one of the options above (type in the number or cancel with CTRL+C): "
        read choice
        case $choice in
            1 )
                user="mm_seg"
                subnet="32"
                submask="20"
                ;;
            2 )
                user="mm_lehrer"
                subnet="48"
                submask="20"
                user="mm_lehrer"
                ;;
            3 )
                user="mm_gast"
                subnet="64"
                submask="20"
                ;;
            4 )
                echo -e "${W}Please enter the ${EMC}ip address${W}:"
                read ip
                user="mm_${ip}"
                echo -e "${W}Please enter the ${EMC}subnet mask ${W}in bit (e.g. 32):"
                read submask
                SQL="INSERT INTO User\
                    VALUES (NULL,'${user}',5,'default','default','mr@hsm.de','passworT',NULL,NULL,'${ip}',${submask},0,1,0,NULL,0,NULL,NULL,0,NULL, NULL, NULL);"
                mysql webgui -e "${SQL}"
                mysql webgui -e "SELECT * FROM User WHERE mixed_mode=1" | grep ${user}
                title "Done" "-"
                user=""
                ;;
            * )
                title "Invalid input" "-"
                ;;
        esac
        if ! [[ -z ${user} ]]; then
            SQL="INSERT INTO User\
                VALUES (NULL,'${user}',5,'default','default','mr@hsm.de','passworT',NULL,NULL,'192.168.${subnet}.0',${submask},0,1,0,NULL,0,NULL,NULL,0,NULL,NULL,NULL);"
            mysql webgui -e "$SQL"
            mysql webgui -e "SELECT * FROM User WHERE mixed_mode=1" | grep ${user}
            title "Done" "-"
        fi
    elif [[ $# -eq 2 ]]; then
        ip=$1
        submask=$2
        user="mm_${ip}"
        SQL="INSERT INTO User (username, first_name, last_name, email, password, ip_net, netmask, mixed_mode)\
            VALUES ('${user}','default','default','mr@hsm.de','passworT','${ip}','${submask}',1);"
        mysql webgui -e "$SQL"
        mysql webgui -e "SELECT * FROM User WHERE mixed_mode=1" | grep $1
    elif [[ $# -eq 3 ]]; then
        SQL="INSERT INTO User\
            VALUES (NULL,'${1}',5,'default','default','mr@hsm.de','passworT',NULL,NULL,'192.168.${2}.0',${3},0,1,0,NULL,0,NULL,NULL,0,NULL,NULL,NULL);"
        mysql webgui -e "$SQL"
        mysql webgui -e "SELECT * FROM User WHERE mixed_mode=1" | grep $1
    fi
}

#################################
# Show User Roles ( muprofile [$user] )
muprofile()
{
    if [[ -z $1 ]]; then
        echo -e "\n${EMB}Shows User Profile(s)."
        echo -e "\n${EMB}Usage:${W}\t\t ${FUNCNAME[0]}${B} \"<username>\""
        echo -e "${EMC}Example:${W}\t muprofile ${B}\"roth\" ${Y}\
            Shows all Users and Roles of all Users containing \"roth\""
    else
        mysql webgui -e "SELECT username, profile_name, profile_override_id, profile_override_name, profile_override_expires, description\
            FROM User,Profile\
            WHERE username LIKE '%${1}%' AND p_id = IFNULL (profile_override_id, profile_id);"
    fi
}

#################################
# Prints ethtool for interface <ethX>
detect_link()
{
  if [[ $(ethtool $1 | grep Link\ detected) == *"yes" ]]; then
    printf  "${EMG}YES"
  else
    printf  "${EMR}NO"
  fi
}

#################################
# Prints ip for interface <ethX>
get_ip()
{
  IP=$(ifconfig $1  2>&1| grep inet | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | head -1)
  if [[ -z ${IP} ]]; then
    printf "${EMR}none\t"
  else
    printf $IP; echo
  fi
}

#################################
# Prints arp-table for offline aps
arpofaps()
{
    for ap in `mysql webgui -e "SELECT * FROM wlan_aps" | cut -d "	" -f 3`
    do
         arp -n | grep $ap
    done
}

#################################
# Prints arp-table for offline aps
arpofax()
{
    for ap in `mysql webgui -e "SELECT * FROM wlan_aps WHERE device_type LIKE 'AX%'" | cut -d "	" -f 3`
    do
         arp -n | grep $ap
    done
}

################################
# Prints surrounding SSIDs which are different to wlan_config ssids
scanWIFI()
{
    echo -en 'gib eine IP ein: ';
    read readip;
    ownSSIDS=$(mysql webgui -e "select * from wlan_config" | grep -i ssid | awk '{print $4}' | xargs echo -en | tr -s ' ' '|');
    searchWLAN=$(ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 $readip iwinfo | grep -B 2 "5.....GHz" | grep -n wlan | grep "^1:" | cut -d ":" -f2 | awk '{print $1}');
    ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 $readip iwinfo $searchWLAN scan | egrep -v $ownSSIDS | grep -B 1 -i ssid
}

#################################
# vimerr <file> - filter 'critical|error|crash|fail|panic'
vimerr()
{
    grep -Pi 'critical|error|crash|fail|panic' $1 | grep -v statistics | vi -c "set ft=messages" -c "set buftype=nowrite" -
}

#################################
# ADD user_notes column to "wlan_aps"-table (fix 3.0.34)
m_add_usernotes()
{
    mysql webgui -e "ALTER TABLE wlan_aps ADD COLUMN `user_notes` text"
}

#################################
# US 03 random joke
octo_joke()
{
	curl -s --insecure https://www.zufallswitz.de/ | grep "bookmark" | grep href\=.**\<\/a | grep -o summary\"\>.**\<\/div\> | cut -d ">" -f2 | cut -d "<" -f1 | sed -e 's/\&\#....\;//'g | sed "s/.\{160\}/&\\n/g"
}

################################
# US 03 random joke20
octo_joke20()
{
  clear; curl -L -s --insecure https://www.witze-fun.de/witze/zufallswitz/ | grep -o '<p>'.* | grep -v rating | grep -o ".*</p>" | sed 's/&quot;//g' | sed 's/&uuml;/ü/g' | sed 's/&szlig;/ß/g' | sed 's/&ouml;/ö/g' | sed 's/&auml;/ä/g' | sed 's/<p>//g' | sed 's/<\/p>//g'
}

#################################
# prüft octo_custedit auf folgenden String: kein managed service
checkthecustedit()
{
if [ -f "/etc/octo/octo_cust.txt" ]; then
kms=$(cat /etc/octo/octo_cust.txt | grep -i "kein managed service" &>/dev/null; echo $?);
if [ “$kms” == “0” ]; then figlet 'kein managed service'; fi
fi
}

#################################
# resetvoucher <eid> RESET Voucher Code
resetvoucher()
{
    if [[ $# -ne 1 ]]; then
        echo -e "\n${EMB}Usage:${W}\t\t${FUNCNAME[0]}  ${B}<eid>"
    else
        mysql webgui -e "UPDATE ExtraLogin
        SET src = NULL,
        dest = NULL,
        status = 0,
        dead_date = NULL,
        start_date = NULL,
        mac = NULL,
        to_process = 0
        WHERE eid=${1}"

        ipset -F OCTOGUEST
        ipset -F OCTOGUEST_SSL
        ipset -F OCTOGUEST_NOSSL

        pf -e
    fi
}

#################################
# deletevoucher <key> DELETE Voucher Code
deletevoucher_key()
{
    if [[ $# -ne 1 ]]; then
        echo -e "\n${EMB}Usage:${W}\t\t${FUNCNAME[0]}  ${B}<key>"
    else
        mysql webgui -e "DELETE from ExtraLogin WHERE \`key\` = '${1}'"
        ipset -F OCTOGUEST_SSL
        ipset -F OCTOGUEST_NOSSL
        pf -e
    fi
}

#################################
# deletevoucher <eid> DELETE Voucher Code
deletevoucher_eid()
{
    if [[ $# -ne 1 ]]; then
        echo -e "\n${EMB}Usage:${W}\t\t${FUNCNAME[0]}  ${B}<eid>"
    else
        mysql webgui -e "DELETE FROM ExtraLogin WHERE eid=${1}"
        ipset -F OCTOGUEST_SSL
        ipset -F OCTOGUEST_NOSSL
        pf -e
    fi
}

#################################
# checks if og-build is up to date
og_build_check()
{
    BUILD_FILE="/octo/sys/octogate-build"
    CURR_BUILD="/tmp/.current_octogate_build"

    if test -e ${CURR_BUILD}
    then
        if grep -q ${OCTO_VER} ${CURR_BUILD}
        then
            if ! diff -q ${BUILD_FILE} ${CURR_BUILD} &> /dev/null
            then
                echo -e "${EMR}WARNING:\t\t\t build version differs from current build!"
            fi
        fi
    fi

}

#################################
# Print first helpful information
mrinfo()
{
    NEW="\n ${R}ovpn_rm_client ${W} -> rm vpn client \n" 
    # ${R}guckma ${W} -> ls -lah \n ${R}machma ${W} -> sh \n ${R}lesma ${W} -> vim \n ${R}octo_joke2038 ${W} -> angucken "

    RAMINFO=$(free -m | awk 'FNR == 3 {print $3 " / " $3+$4}')
    WB=$(wbinfo -t 2>&1 )
    WBVAR="${EMR}FAILED"
    CNT_USERS=0
    UPTIME=$(uptime | sed 's/,/ /g' | awk '{print $3,$4}')

    test -f /data/.maintenance/migration.done \
        && LAST_UPDATED="$(cat /data/.maintenance/migration.done | grep Date | sed 's/Date:\s*//')"

    if [[ $WB == *"succeeded" ]]; then
        WBVAR="${EMG}OK"
        CNT_USERS=$(wgui "SELECT COUNT(*) AS Anzahl FROM User WHERE is_ad_user=1" | grep -E '[0-9]+')
    fi

    title "INFOs by mr script" "#"
    echo -e "${W}CUSTOM aliases etc. type ${R}mrhelp${W} to get a detailed list"
    echo -e "${W}or use ${R}mrinfo${W} to view these infos again"

    captPortCheck

    title "rc-status Info" "-"
    echo -e "${R}$(rc-status | egrep crashed\|inactive\|error\|stopped|\
        sed -e 's/ \+/ /g')"

    title "WIFI Info" "-"
    echo -e "${W}APs ONLINE:\t\t\t ${EMG}`aps | grep ONLINE | wc -l`"
    echo -e "${W}APs OFFLINE:\t\t\t ${EMR}$((`aps | grep -v ONLINE | grep -vi virtual | grep -v device | wc -l`))"
    #echo -e "${W}APs OFFLINE:\t\t\t ${EMR}$((`aps | grep OFFLINE | grep -vi virtual | wc -l`))"

    local result="$(mysql webgui -Ne 'select status from wlan_aps' | grep CS )"

    if [[ ! -z $result ]]
    then
            echo "Caching Server Active"
    fi


    if [ "$OCTO_AD_PASSWORD" != "" ]; then
         title "AD Info" "-"
         echo -e "${EMC}AD Server: \t\t\t ${EMG}${OCTO_AD_SERVER} ( ${OCTO_AD_SERVER_IP} )"
         printf "%s %17s : %5s\n"  "${OCTO_AD_DOMAIN}" "${OCTO_AD_ADMINISTRATOR}" "${OCTO_AD_PASSWORD}" # old values 36s 10s
         echo -e "${W}Users imported from AD: \t ${EMG}${CNT_USERS}"
         echo -e "${W}wbinfo -t\t\t\t ${WBVAR}"
    fi

    title "IP Info" "-"
    echo -e "${W}IP ${EMM}eth0\t${EMG}$(get_ip eth0)${W}\t\t INT\tLink:\t$(detect_link eth0)"
    echo -e "${W}IP ${EMM}eth1\t${EMG}$(get_ip eth1)${W}\t\t DMZ\tLink:\t$(detect_link eth1)"
    echo -e "${W}IP ${EMM}eth2\t${EMG}$(get_ip eth2)${W}\t\t WIFI\tLink:\t$(detect_link eth2)"
    echo -e "${W}IP ${EMM}eth3\t${EMG}$(get_ip eth3)${W}\t\t EXT\tLink:\t$(detect_link eth3)"
    if [[ $(ip address show eth4 2> /dev/null) ]]
    then
        echo -e "${W}IP ${EMM}eth4\t${EMG}$(get_ip eth4)${W}\t\t SP1\tLink:\t$(detect_link eth4)"
    fi
    if [[ $(ip address show eth5 2> /dev/null) ]]
    then
        echo -e "${W}IP ${EMM}eth5\t${EMG}$(get_ip eth5)${W}\t\t SP2\tLink:\t$(detect_link eth5)"
    fi
    if [[ $(ip address show eth6 2> /dev/null) ]]
    then
        echo -e "${W}IP ${EMM}eth6\t${EMG}$(get_ip eth6)${W}\t\t SP3\tLink:\t$(detect_link eth6)"
    fi
    if [[ $(ip address show eth7 2> /dev/null) ]]
    then
        echo -e "${W}IP ${EMM}eth7\t${EMG}$(get_ip eth7)${W}\t\t SP4\tLink:\t$(detect_link eth7)"
    fi
    if [[ $(ip address show eth8 2> /dev/null) ]]
    then
        echo -e "${W}IP ${EMM}eth8\t${EMG}$(get_ip eth8)${W}\t\t SP5\tLink:\t$(detect_link eth8)"
    fi
    if [[ $(ip address show eth9 2> /dev/null) ]]
        then
	        echo -e "${W}IP ${EMM}eth9\t${EMG}$(get_ip eth9)${W}\t\t SP6\tLink:\t$(detect_link eth9)"
		    fi
    echo -e "${W}IP ${EMM}tun0\t${EMG}$(get_ip tun0)${C} :8080\t${W} TUN0${W}\tManagement / Proxy"
    echo -e "${W}IP ${EMM}tun1\t${EMG}$(get_ip tun1)${C}\t\t${W} OpenVPN Server${W}"
    echo -e "${W}IP ${EMM}tun2\t${EMG}$(get_ip tun2)${C}\t\t${W} OpenVPN Client${W}"
    echo -e "${W}Link WebGUI:${W}\t\t\t https://$(get_ip tun0):444"

    title "SYS Info" "-"
    df -h | egrep sda6\|hda6 | awk -v red="$(tput setaf 1)" -v green="$(tput setaf 2)"\
        -v yellow="$(tput setaf 3)" -v reset="$(tput sgr0)" \
        '{ if ($5>90) color=red; else if ($5>80) color=yellow; else color=green
        printf "%s%s%s on %s%s\t\t %s%s\n",color, $1, reset, color, $6, $5, reset }'
    if [[ $(grep "sda6.*\sro[\s,]" /proc/mounts) ]]; then
        echo -e "${EMR}### WARNING: SDA6 IS READONLY!!! ###"
    fi
    FILE=/etc/octo/confdeploy.txt
    if test -f "$FILE"; then
     cat $FILE | tr '\n' ' '| grep -i lmz &>/dev/null;
     if [ $? == "0" ]; then
      depv=$(cat $FILE | tr '\n' ' '| grep -i lmz| awk '{print $2}')
      echo -e "Confd-Ver.:\t\t\t $depv"
     fi;
    fi

    if [ "$LAST_UPDATED" != "" ]; then
      echo -e "${W}Last Update:\t\t\t ${W}${LAST_UPDATED}"
    fi
    echo -e "${W}Uptime:\t\t\t\t ${W}${UPTIME}"
    echo -e "${W}RAM:\t\t\t\t ${W}${RAMINFO} MB used"
    echo -e "${W}Arch:\t\t\t\t ${W}$(uname -m)"
    og_build_check

    cat /var/log/mysql/mysqld.err | grep --colour=auto -i error | grep -v communication | grep -v semantics &>/dev/null;
    if [ $? == "0" ]; then
        title "last 2 mysql ERRORs" "-"
        echo -ne "${R}"
    	cat /var/log/mysql/mysqld.err | grep -n --colour=auto -i error | grep -v communication | grep -v semantics | tail -2
    fi;

    title "SSH connections" "-"
    a=$(ps -aux | grep -i tmp | grep -i SSH_USR | cut -d "'" -f 2); echo -en "Angemeldete Socto-User:(" $a "),"
    echo -en " gesamte Anzahl SSH-User: "; Anz_Us=$(ps -aux | grep -i sshd: | grep -v grep | wc -l); echo $Anz_Us

    ls /etc/octo/opf.d/08_icmp &>/dev/null || ls /data/havp_fix/havpwhite.txt &>/dev/null;
    if [ $? == "0" ]; then
        title "inserted rules" "-"
        echo -ne "${R}"
    ls /etc/octo/opf.d/08_icmp &>/dev/null;
     if [ $? == "0" ]; then
        echo -e "${W} nextcloud_fix ${EMG}\t\t\t active"

     fi;
     ls /data/havp_fix/havpwhite.txt &>/dev/null;
     if [ $? == "0" ]; then
        echo -e "${W} havp_fix ${EMG}\t\t\t active"

     fi;
    fi;
    find_wrong200_bbb_rules

    title "END" "#"

    echo -e ">>> Newest feature: ${NEW}"
    checkthecustedit
}


#################################
# builds base-DN from OCTO-vars
get_bind_dn()
{
    echo $OCTO_AD_DOMAIN | tr '.' '\0' | xargs -0 printf "dc=%s," | sed -E 's/,$//'
}

#################################
# builds admin-DN from OCTO-vars
get_admin_dn()
{
    local BINDDN=$(get_bind_dn)
    echo "CN=${OCTO_AD_ADMINISTRATOR},CN=Users,${BINDDN}"
}

#################################
# returns DN of given object
get_dn_of()
{
    local BINDDN=$(get_bind_dn)

    local LDAP_OPT="-x -LLL"
    LDAP_OPT="${LDAP_OPT} -h ${OCTO_AD_SERVER}"
    LDAP_OPT="${LDAP_OPT} -D $(get_admin_dn)"
    LDAP_OPT="${LDAP_OPT} -w ${OCTO_AD_PASSWORD}"
    LDAP_OPT="${LDAP_OPT} -b ${BINDDN}"

    local FOUND_DN=$(ldapsearch ${LDAP_OPT} ${1} dn | grep ^dn | cut -d " " -f 2)

    echo "${FOUND_DN}"
}

#################################
# Confirm-Loop
confirm()
{
    title "$1" "-"

    while true
    do
        read -r -p "Are You Sure? [Y/n] " input

        case $input in
            [yY][eE][sS]|[yY])
                return 0
                ;;
            [nN][oO]|[nN])
                return 1
                ;;
            *)
                echo "Invalid input..."
                ;;
        esac
    done
}

#################################
# echo ldap-options for ldapmodify / ldapsearch queries
get_ldap_opts()
{
    local BINDDN=$(get_bind_dn)

    local LDAP_OPT="-h ${OCTO_AD_SERVER_IP}"
    LDAP_OPT="${LDAP_OPT} -b ${BINDDN}"

    ########################################

    if [[ -z $2 ]]
    then
        LDAP_OPT="${LDAP_OPT} -D $(get_admin_dn)"
        LDAP_OPT="${LDAP_OPT} -w ${OCTO_AD_PASSWORD}"
    else
        local USER_DN=$(get_dn_of "sAMAccountname=$1")
        LDAP_OPT="${LDAP_OPT} -D ${USER_DN}"
        LDAP_OPT="${LDAP_OPT} -w $2"
    fi

    ########################################

    echo "${LDAP_OPT}"
}

#################################
# check_ldap_auth [user] [pw] - checks if ldap-bind is possible
check_ldap_auth()
{
    local LDAP_OPT=$(get_ldap_opts "$1" "$2")
    title "testing ldap-auth" "-"

    set -x
    ldapsearch ${LDAP_OPT}
    set +x
}

#################################
# change_ldap_pw <user> <new-pw> - use ldapmodify to change a user's password
change_ldap_pw()
{
    if [[ -z $2 ]]
    then
        echo -e "\n${EMB}Usage:${W}\t\t change_ldpa_pw ${B}\"<user>\" ${B}\"<password>\""
        echo -e "${W}"
        return 1
    fi

    ########################################

    local ADMIN_DN=$(get_admin_dn)
    local HOST="${OCTO_AD_SERVER_IP}"

    local LDIF_FILE="/tmp/ld_pw_change.ldif"
    local LDAP_OPT="-h ${OCTO_AD_SERVER_IP} "
    LDAP_OPT="${LDAP_OPT} -D ${ADMIN_DN}"
    LDAP_OPT="${LDAP_OPT} -w ${OCTO_AD_PASSWORD}"

    local USER_DN=$(get_dn_of "sAMAccountname=$1")
    local UNICODE_PWD=$(echo $2 | base64)

    ########################################

    echo "dn: ${USER_DN}"              >  ${LDIF_FILE}
    echo "changetype: modify"          >> ${LDIF_FILE}
    echo "replace: unicodePwd"         >> ${LDIF_FILE}
    echo "unicodePwd::${UNICODE_PWD}"  >> ${LDIF_FILE}

    ########################################

    cat ${LDIF_FILE}
    confirm "Do you really want to execute the above changes?" \
        || return 1
    set -x
    ldapmodify -f ${LDIF_FILE} ${LDAP_OPT}
    set +x

    check_ldap_auth "$1" "$2"
}

#################################
# rdptables <rdp-host> - create iptables rule for RDP
rdptables()
{
    iptables -t nat -A PREROUTING -i tun0 -p tcp --dport 3389 -j DNAT --to ${1}:3389
}

#################################
# chkcontentfilter <domain> - show hit-value from blacklist
chkcontentfilter()
{
    if [[ -z $1 ]]; then
        title "CONTENTFILTER" "-"
        echo -e "\n${EMB}Usage:${W}\t\t chkcontentfilter ${B}\"<domain>\""
        echo -e "${EMC}Example:${W}\t chkcontentfilter ${C}\"porn.com\""
        echo -e "${W}"
        mysql contentfilter -e "SELECT getBlackList(REVERSE('porn.com'))"
        title "END" "-"
    else
        mysql contentfilter -e "SELECT getBlackList(REVERSE('${1}'))"
    fi
}

#################################
# mmux handle tmux sessions
mmux()
{
    history -a;

    if test ! "$TMUX"
    then
        tmux -f /tmp/.tmux.conf new-session -d 'bash --rcfile /tmp/.bashrc_temp'
        tmux a
    else
        tmux split-window -h -d 'bash --rcfile /tmp/.bashrc_temp'
    fi
}

#################################
# Kills all detached tmux sessions
tsesski()
{
	tmux list-sessions | grep -v attached | cut -d: -f1 |  xargs -t -n1 tmux kill-session -t
}

#################################
# aktive MAC aus IPTABLES-SAVE in ExtraLogin mit gültigem Ablaufdatum iptables-save
printVoucher_uV38()
{
    local macs=$(iptables-save | grep NAT_HOTEL_FW | awk '{print $8}' | grep -v tcp | sort -u | xargs echo -en | tr -s ' ' '|');
    echo;
    echo -en 'MACs aus IPTABLES-SAVE in ExtraLogin: ';
    echo;
    mysql webgui -e "SELECT * FROM ExtraLogin WHERE dead_date >= DATE(NOW())" | egrep -i $macs | awk "{print \$4, \$9, \$16}"
}

#################################
# growth check - prüft in /var/log alle Verzeichnis über X Sekunden auf das Wachstum
printGrowth()
{
    E='echo -e';
    e='echo -en';
    nodir='^d[a-zA-Z0-9]';
    nodot='^/.';
    lion='\033[36m';
    lioff='\033[0m';
    dcd='/root/';
    nolines ()
    {
        grep --colour=auto -v '^\s*$'
    };
    logroot ()
    {
        cd /var/log
    };
    logog ()
    {
        cd /var/log/octogate
    };
    tabs ()
    {
        column -t
    };
    givelist ()
    {
        ls --color=auto -laR | deldir | awk '{print $9, $5, $1}' | awk '{print $1, $2}' | tabs | delke | delto
    };
    delke ()
    {
        grep --colour=auto -v "\.keep"
    };
    delto ()
    {
        grep --colour=auto -v "total"
    };
    deldir ()
    {
        grep --colour=auto -v '^d[a-zA-Z0-9]'
    };
    magic ()
    {
        touch /root/dsum;
        logroot;
        givelist > $dcd\01;
        sleep $zz;
        logroot;
        givelist > $dcd\02;
        $E $lion\\t/var/log/ Growth Check 60 Sekunden$lioff > $dcd\dsum;
        diff -uNr $dcd\01 $dcd\02 | grep --colour=auto -v "\@" | egrep '(^\+|^\-|^ ./)' | grep --colour=auto -v root >> $dcd\dsum;
        rm $dcd\01 $dcd\02;
        cat $dcd\dsum | egrep $(cat $dcd\dsum | awk '{print substr($0,2)}' | awk '{print $1}' | sort -d | uniq -d | xargs echo -en | tr -s ' ' '|') | sort -d > $dcd\auswertung;
        $E $lion\ "$(date) - Growth Check $zz Sekunden: bei /var/log/" $lioff;
        echo;
        cat $dcd\auswertung | awk 'NR%2{a=$0;next}{print a" "$0}' | column -t | awk '{print $1,$2/1024/1024" MB",$4/1024/1024" MB","Differenz: " ($4-$2)}' | column -t
    };
    hdr ()
    {
        if [ $1 = "tr" ]; then
            echo -e "\t----------------------------------------------------------------------------------\033";
        else
            satz=$(echo "$@");
            al=$(echo ${#satz});
            b=$(expr 80 - $al);
            cl=$(echo $(( $b / 2 )) );
            rest=$(echo $(( $b % 2 )) );
            links=$cl;
            rechts=$links;
            echo -en "\t";
            while [ $links != 0 ]; do
                echo -en '-';
                let links=$links-1;
            done;
            echo -en "\033[36m[\033[32m"$satz"\033[36m]\033[0m";
            if [ $rest -gt "0" ]; then
                echo -en '-';
            fi;
            while [ $rechts != 0 ]; do
                echo -en '-';
                let rechts=$rechts-1;
            done;
            echo;
        fi
    };
    dosth ()
    {
        if [[ -z $@ ]]; then
            echo -e "$(hdr $(echo 'Infotext der Ausrufe'))";
            echo -e "\t Aufruf: \033[42m"printGrowth"\033[0m [\033[36m-option\033[0m] (optional)";
            echo -e "\t \033[36mprintGrowth + Zahl\033[0m \t\t\t :\t prüft über X Sekunden auf Differenzen";
            echo;
            echo -e "\t \033[35m*: Dateien dsum und auswertung werden in /root/ angelegt\033[0m";
            echo -en "\033[32m\t---------------------------------------------------------------\033[5m Vers.a.002 \033[0m";
            echo -e "\033[32m------- \033[0m";
            echo $@;
        else
            zz=$@;
            magic;
        fi
    };
    dosth $@
}


#################################
# aktive MAC aus IPTABLES-SAVE in ExtraLogin mit gültigem Ablaufdatum ipset -L
printVoucher_aV38()
{
    macs=$(ipset -L OCTOGUEST_NOSSL | grep ":..:" | sort -u | xargs echo -en | tr -s ' ' '|');
    echo;
    echo -en 'MACs aus IPTABLES-SAVE in ExtraLogin: ';
    echo;
    mysql webgui -e "select * from ExtraLogin WHERE dead_date >= DATE(NOW())" | egrep -i $macs | awk "{print \$4, \$9, \$16}"
}

################################
# checkSNV prüft ob alles korrekt hinterlegt ist
checkSNV()
{
    # Einleitung SNV-Check, VZbuilt und wechsel
    clear;
    mkdir /tmp/snvcheck;
    cd /tmp/snvcheck;
    echo -e "\033[36m SNV-Check \033[0m";
    # host = inl?
    cat /var/log/octogate/octo-vpn.nfo | grep --colour=auto -i inl &>/dev/null;
    if [ $? == "0" ]; then
        echo -e "Host? \033[36m[\033[0m\033[32m INL \033[36m]\033[0m" >> ck.status;
    else
        echo -e "Host=INL? \033[36m[\033[0m\033[31m WRONG \033[36m]\033[0m" >> ck.status;
    fi;
    # module aktiv?
    cat /etc/octo/modules.conf | grep --colour=auto 'MENT="1"' &>/dev/null;
    if [ $? == "0" ]; then
        echo -e "Module \033[36m[\033[0m\033[32m angepasst \033[36m]\033[0m" >> ck.status;
    else
        echo -e "Module \033[36m[\033[0m\033[31m unvollständig \033[36m]\033[0m" >> ck.status;
    fi;
    # wbinfo ok?
    ( wbinfo -t &>/dev/null );
    if [ $? == "0" ]; then
        echo -e "wbinfo \033[36m[\033[0m\033[32m angepasst \033[36m]\033[0m" >> ck.status;
    else
        echo -e "wbinfo \033[36m[\033[0m\033[31m Error \033[36m]\033[0m" >> ck.status;
    fi;
    # hosteinträge vorhanden?
    cat /etc/hosts | grep --colour=auto -i websnv &>/dev/null;
    if [ $? == "0" ]; then
        echo -e "hostseintrag \033[36m[\033[0m\033[32m vorhanden \033[36m]\033[0m" >> ck.status;
        snvIP=$(cat /etc/hosts | grep snv | awk '{print $1}');
    else
        echo -e "hostseintrag \033[36m[\033[0m\033[31m unvollständig \033[36m]\033[0m" >> ck.status;
    fi;
    # cron-cifs (mount check)
    cat /etc/octo/octocron | grep --colour=auto -i cifs &>/dev/null;
    if [ $? == "0" ]; then
        echo -e "cron-cifs \033[36m[\033[0m\033[32m vorhanden \033[36m]\033[0m" >> ck.status;
    else
        echo -e "cron-cifs \033[36m[\033[0m\033[31m fehlt \033[36m]\033[0m" >> ck.status;
    fi;
    # opf.d - timago regeln vorhanden?
    cat /etc/octo/opf.d/* | grep --colour=auto -i timago &>/dev/null;
    if [ $? == "0" ]; then
        echo -e "opf.d-Timago \033[36m[\033[0m\033[32m vorhanden \033[36m]\033[0m" >> ck.status;
    else
        echo -e "opf.d-Timago \033[36m[\033[0m\033[31m fehlt \033[36m]\033[0m" >> ck.status;
    fi;
    # netconf - byod url gesetzt?
    bb=$(cat /etc/octo/octonet.conf | grep -i byod_back | cut -d \" -f2 | grep http);
    if [[ -z $bb ]]; then
        echo -e "netconf-byodURL \033[36m[\033[0m\033[31m fehlt \033[36m]\033[0m" >> ck.status;
    else
        echo -e "netconf-byodURL \033[36m[\033[0m\033[32m vorhanden \033[36m]\033[0m" >> ck.status;
    fi;
    # netconf - byod IF gesetzt?
    bb=$(cat /etc/octo/octonet.conf | grep -v "\#"| grep -i byod_if | cut -d \" -f2 | grep -v BYOD_IF);
    if [[ -z $bb ]]; then
        echo -e "netconf-byodIF \033[36m[\033[0m\033[31m fehlt \033[36m]\033[0m" >> ck.status;
    else
        echo -e "netconf-byodIF \033[36m[\033[0m\033[32m vorhanden \033[36m]\033[0m" >> ck.status;
    fi;
    # nginx im runlvl?
    cat /etc/octo/services.conf | grep --colour=auto -i nginx &>/dev/null;
    if [ $? == "0" ]; then
        echo -e "nginx-im-rc \033[36m[\033[0m\033[32m vorhanden \033[36m]\033[0m" >> ck.status;
    else
        echo -e "nginx-im-rc \033[36m[\033[0m\033[31m fehlt \033[36m]\033[0m" >> ck.status;
    fi;
    # nginx PIDS?
    nginalive=$(echo -en "nginx-PIDS_mast/work: \033[36m[\033[0m\033[32m "; ps -aux | grep nginx | egrep '(master|worker)' | awk '{print $2}' | xargs echo -en | tr -s ' ' ',');
    ps -aux | grep --colour=auto nginx | egrep '(master|worker)' &>/dev/null;
    if [ $? = "0" ]; then
        echo -e "$nginalive \033[36m]\033[0m" >> ck.status;
    else
        echo -e "nginx \033[36m[\033[0m\033[31m läuft_nicht \033[36m]\033[0m" >> ck.status;
    fi;
    # pfconf portfreischaltungen vorhanden?
    cat /etc/octo/octopf.conf | grep --colour=auto -v DNAT | grep --colour=auto -i $snvIP &>/dev/null;
    if [ $? == "0" ]; then
        echo -e "pfconf_portfs \033[36m[\033[0m\033[32m vorhanden \033[36m]\033[0m" >> ck.status;
    else
        echo -e "pfconf_portfs \033[36m[\033[0m\033[31m fehlen \033[36m]\033[0m" >> ck.status;
    fi;
    # pfconf dnat vorhanden?
    cat /etc/octo/octopf.conf | grep --colour=auto -A5 DNAT_SERVICE | grep --colour=auto -v \# | grep --colour=auto -B 1 \) | grep --colour=auto -v SNAT | grep --colour=auto -i $snvIP &>/dev/null;
    if [ $? == "0" ]; then
        echo -e "pfconf_dnat \033[36m[\033[0m\033[32m vorhanden \033[36m]\033[0m" >> ck.status;
    else
        echo -e "pfconf_dnat \033[36m[\033[0m\033[31m fehlt \033[36m]\033[0m" >> ck.status;
    fi;
    # Gäste - MMUser vorhanden?
    mysqlcheck=$(mysql webgui -e 'SELECT * FROM User WHERE mixed_mode=1');
    echo $mysqlcheck | grep --colour=auto -P '192\.168\.(6([4-9])|(7[0-9]))' &>/dev/null;
    if [ $? == "0" ]; then
        echo -e "vlan2410_MM_U \033[36m[\033[0m\033[32m vorhanden \033[36m]\033[0m" >> ck.status;
    else
        echo -e "vlan2410_MM_U \033[36m[\033[0m\033[31m fehlt \033[36m]\033[0m" >> ck.status;
    fi;
    # GastIF Transparenter Proxy aktiv?
    cat /etc/octo/octopf.conf | grep --colour=auto 'VLAN_AP2410=1' | cut -d = -f 2 | grep --colour=auto 1 &>/dev/null;
    if [ $? == "0" ]; then
        echo -e "GASTIF_TProxy \033[36m[\033[0m\033[32m aktiv \033[36m]\033[0m" >> ck.status;
    else
        echo -e "GASTIF_TProxy \033[36m[\033[0m\033[31m inaktiv \033[36m]\033[0m" >> ck.status;
    fi;
    # BYODIF Transparenter Proxy aktiv?
    bb=$(cat /etc/octo/octonet.conf | grep -v "\#"| grep -i byod_if | cut -d \" -f2 | grep -v BYOD_IF);
    bIFFin=$(bIFck=$(echo -e $bb"=1="); cat /etc/octo/octopf.conf | grep $bIFck | cut -d = -f 2 | grep 1);
    if [[ ! -z $bb ]] && [ $bIFFin == "1" ]; then
        echo -e "byodIF_TProxy \033[36m[\033[0m\033[32m aktiv \033[36m]\033[0m" >> ck.status;
    else
        echo -e "byodIF_TProxy \033[36m[\033[0m\033[31m inaktiv \033[36m]\033[0m" >> ck.status;
    fi;
    # Ausgabe einleiten und VZ wieder bereinigen
    cat ck.status | column -t;
    cd;
    rm -r /tmp/snvcheck;
    echo done;
    # Variablen leeren
    unset bb;
    unset bIFck;
    unset bIFFin;
    unset mysqlcheck;
    unset nginalive;
    unset snvIP
}

################################
# gibt zur conf deploy version die entsprechende Zielversion heraus
getVersionLMZ()
{
    deploy_version="?";
    deploy_cust="?";
    if [ -f "/octo/etc/confdeploy.txt" ]; then
        deploy_cust=`cat /octo/etc/confdeploy.txt | grep customer | cut -d "=" -f 2`;
        deploy_version=`cat /octo/etc/confdeploy.txt | grep version | cut -d "=" -f 2`;
    fi;
    echo -en "- aktuelle Daten - \nConfDeployVersion:\033[36m$deploy_version\033[0m, Typ:\033[36m$deploy_cust\033[0m, Version:\033[36m${OCTO_VER}\033[0m, Hd.Name:\033[36m${OCTO_DEAL}\033[0m \n\n";
    getVersioncatch ()
    {
      case $userip in
        "19")
          echo "3.0.34"
        ;;
        "20")
          echo "3.0.35"
        ;;
        "21")
          echo "3.0.36"
        ;;
        "22")
          echo "3.0.38 (reboot nach update)"
        ;;
        "23")
          echo "3.0.39/3.0.42 (neu)"
        ;;
        "24")
        echo "3.0.42"
        ;;
        "25")
          echo "3.0.42 (reboot nach update)"
        ;;
        "26")
          echo "3.0.43"
        ;;
        "27")
          echo "3.0.44"
        ;;
        *)
          echo -e "\033[31m[ Unknown - überprüfe deine Eingabe! ]\033[0m"
        ;;
      esac
    };
    if [[ -z $@ ]]; then
        echo -en "Welche Confdeployversion hat deine Maschine ?: ";
        read -p "" userip;
        echo -en "dann sollte Sie Version ";
        echo -en $(getVersioncatch $userip) "sein. \n";
    else
        echo -en "zu der Confdeployversion $@ sollte die Version entsprechend" $(getVersioncatch $@) "sein. \n";
    fi
}

#################################
# Zeige Surfverhalten der User
suh()
{
    touch /root/test.txt;
    touch /root/final.txt;
    whosurfed=$(cat /var/log/squid/access.log | grep TCP_MISS/200 | grep "GET" | awk '{print $5, $10}' | sort -u | sort -d | grep -v "-");
    if [[ $whosurfed == "" ]]; then
        echo "/var/log/squid/access.log enthält keine passenden Einträge" > /root/test.txt;
    else
        echo "$whosurfed" > /root/test.txt;
    fi;
    read -a websurfer -d "" < /root/test.txt;
    linebreaker=`expr 1`;
    for user in "${websurfer[@]}";
    do
        if [[ $linebreaker == "14" ]]; then
            linebreaker=`expr 1`;
            echo -en "$catch \n" >> /root/final.txt;
            catch=$(echo "");
        else
            linebreaker=`expr $linebreaker + 1`;
            punkt=$(echo $user | grep -i "...\...\.....");
            catch=$(echo -en "$catch $user");
        fi;
    done;
    cat /root/final.txt | column -t;
    echo;
    read -p "Eingabe zu suchender User: " userip;
    echo "Angesurfte Webseiten der UserIP: ";
    echo;
    cat /var/log/squid/access.log | grep --colour=auto $userip | grep --colour=auto TCP_MISS/200 | grep --colour=auto "GET" | awk '{print $5, $9, $10}' | sort -u | sort -d;
    rm /root/test.txt;
    rm /root/final.txt
}

#################################
# ldcheck [-g <group>, -u <username>] - displays details about group members or user-info
ldcheck()
{
    local BINDDN=$(get_bind_dn)
    local ADMIN_DN="CN=${OCTO_AD_ADMINISTRATOR},CN=Users,${BINDDN}"
    local ARG="givenName"

    echo -e "${W}BINDDN is: ${C}${BINDDN}${W}"

    ########################################

    local OPTIND opt
    while getopts "u:g:" opt
    do
        case "$opt" in
            u)  ARG="(sAMAccountname=${OPTARG})"
                echo -e "${W}AD_FILTER: ${C}${ARG}${W}"
                ;;

            g)  local GRP_DN=$(ldapsearch -x -LLL -h ${OCTO_AD_SERVER} -D ${ADMIN_DN} -w ${OCTO_AD_PASSWORD} -b"${BINDDN}" | \
                    perl -p00e 's/\r?\n //g' | grep -i ${OPTARG} | head -1 | cut -d ' ' -f 2)
                ARG="(memberOf:1.2.840.113556.1.4.1941:=${GRP_DN})"
                ARG="${ARG} distinguishedName"
                echo -e "${W}AD_FILTER: ${C}${ARG}${W}"
                ;;
        esac
    done

    ########################################

    echo -e "${W}AD_IP    : ${C}${OCTO_AD_SERVER_IP}${W}"
    echo -e "${W}AD_SERVER: ${C}${OCTO_AD_SERVER}${W}"
    echo -e "${W}AD_ADMIN : ${C}${ADMIN_DN}${W}"
    echo -e "${W}AD_PASSWD: ${C}${OCTO_AD_PASSWORD}${W}"

    ########################################

    ldapsearch -o ldif-wrap=no -x -LLL -h ${OCTO_AD_SERVER} -D "${ADMIN_DN}" -w ${OCTO_AD_PASSWORD} -b"${BINDDN}" ${ARG}

    ########################################

    echo -en "${W}Query was: "
    echo -e  "${C}ldapsearch -o ldif-wrap=no -x -LLL -h ${OCTO_AD_SERVER} -D \"${ADMIN_DN}\" -w ${OCTO_AD_PASSWORD} -b\"${BINDDN}\" ${ARG}"
}

#################################
# Check for missing ipset-MAC Addresses
missing_voucher_mac()
{
    local query='SELECT `mac`, `key`, start_date , dead_date FROM ExtraLogin WHERE `mac` IS NOT NULL AND dead_date > NOW()'
    local tmp_voucher_file="/tmp/active_voucher.txt"
    local cnt=0

    mysql webgui -e "${query}"  > "${tmp_voucher_file}"

    echo -e "searching for active voucher-MACs ${R}NOT${RST} in ipset:"

    for mac in $(cat "${tmp_voucher_file}" | cut -f 1)
    do
        if [ $(ipset -L | grep -i $mac | wc -l) -lt 1 ]
        then
            ((cnt++))
            echo "$mac missing"
        fi
    done
    echo -e "${C}total:${RST} ${cnt}"
}


#################################
# print passwords i can't remember
pws()
{
    title "Passwords" "-"
    echo -e "${W}Webgui 2.4.3\t ${Y}Ree7quai"
    echo -e "${W}Webgui 3.x.x\t ${Y}ree5Thai"
    echo -e "${W}Tine Setup\t ${Y}tinesetup ${W}: ${Y}aifahY6i"
    echo -e "${W}Tine Admin\t ${Y}tine.admin ${W}: ${Y}Nen5quai"
    echo -e "${W}Tine DB\t\t ${Y}tine ${W}: ${Y}osh2Yain"
    echo -e "${W}igore\t\t ${Y}ree7Choo"
    echo -e "${W}WebGUI-DB:\t ${Y}webgui ${W}: ${Y}EiSei2Mo"
    echo -e "${W}n.A.:\t\t ${Y}Tei0raew ${W}, ${Y}GKpte1kc ${W}"
    echo -e "${W}octo_setup:\t ${Y}Ach9Yagh"
    echo -e "${W}MySQL:\t\t ${Y}Fi0geeth"
    echo -e "${W}BIOS Fitlet:\t ${Y}XgSmsWKv"
    echo -e "${W}AP-Labor:\t ${Y}exchange\\labor ${W}: ${Y}HsmOctoGate2019!"
    title "END" "-"
}

#################################
# writes "DATE SSH_USR CMD" to /root/.octo_history (used by PROMPT_COMMAND)
write_history()
{
    HIST_DATE=$(/bin/date "+%Y-%m-%d %H:%M:%S")
    LAST_COMMAND=$(tail -1 /root/.bash_history)
    printf "%s - %s: %s\n" "${HIST_DATE}" "${SSH_USR}" "${LAST_COMMAND}" >> ${OCTO_HISTORY_FILE}
}

#################################
# adds uci string to webgui.wlan_config for 2nd radius on port 1813 (see GMBK)
add_uci_string()
{
    mysql webgui -e "INSERT INTO `wlan_config` VALUES (555,1,'',NULL,NULL,'set wireless.@wifi-iface[1].port=\'1813\'\nset wireless.@wifi-iface[4].port=\'1813\'');"
}

#################################
# sets webgui.wlan_aps.command to CONFIG_SYNC (WARNING: for all APs!)
ap_config_sync()
{
    # TODO: how about an optional argument for an id / mac???
    mysql webgui -e "UPDATE wlan_aps SET command = \"CONFIG_SYNC\""
}

#################################
# ap_repair <ethX> <ap-ip> <spoof-ip> <og-ip> - Fix/Repair Bootloader-bug
ap_repair()
{
    if [[ -z $3 ]]; then
        echo -e "\n${EMB}Usage:${W}\t\t ap_repair ${B}\"<interface>\" \"<apip>\" \"<spoof-ip>\" "
        echo -e "${EMC}Example:${W}\t ap_repair ${C}\"eth0\" \"192.168.60.2\" \"0.0.0.6\""
        echo -e "${W}"
    else
        IFACE=${1}
        AP_IP=${2}
        SP_IP=${3}
        OG_IP=$(get_ip ${IFACE})
        title "fixing ${AP_IP} on ${IFACE} with spoofing IP ${SP_IP}" "-"
        echo -e "Applying iptables-Rules:"
        set -x
        iptables -t nat -I PREROUTING -i ${IFACE} -s ${AP_IP} -d ${SP_IP} -j DNAT --to-destination ${OG_IP}
        iptables -I INPUT -i ${IFACE} -s ${AP_IP}/16 -j ACCEPT
        iptables -I OUTPUT -d ${AP_IP}/16 -j ACCEPT
        ip address add ${SP_IP} dev ${IFACE}
        arping -I ${IFACE} ${AP_IP} -A -s ${SP_IP}
        set +x
        title "END" "-"
    fi
}

#################################
# ap_analyze <AP-mac> : (NOTE:not fully implemented; => use ap_repair afterwards)
ap_analyze()
{
    if [[ -z ${1} ]]; then
        echo -e "\n${EMB}Usage:${W}\t\t ${FUNCNAME[0]} ${B}\"<MAC>\""
        echo -e "${EMC}Example:${W}\t ${FUNCNAME[0]} ${C}\"42:a5:ef:8b:23:00\""
        echo -e "${W}"
    else
        mac=${1}
        title "Analysis for ${EMB}${mac}${C}" "="

        iface=$(arp -n | grep ${mac} | awk '{print $5}')
        ip=$(arp -n | grep ${mac} | awk '{print $1}')
        echo -e "arpinging ${EMY}${ip}${W}... (2sec)"
        arping -I ${iface} ${ip} -w 2

        if [[ -z $iface ]]; then
            echo -e "${EMB}${mac} ${EMR}doesn't seem to be connected."
            echo -e "${EMB}Ethtool output:"
            echo -e "${W}LINK on eth0: $(detect_link eth0)"
            echo -e "${W}LINK on eth1: $(detect_link eth1)"
            echo -e "${W}LINK on eth2: $(detect_link eth2)"
        else
            title "${mac} has ${ip} on ${iface}" "."
            echo -e "Do you wanna start a tcpdump?(y/n)"
            read choice
            case $choice in
                "y" )
                    tcpdump -eni ${iface} | grep -v GRE | grep ${mac} | head -5
                ;;
            esac
            echo -e "Do you wanna fix this AP?(y/n)"
            read choice
            case $choice in
                "y" )
                    echo -e "finding IP to spoof (this takes some seconds)..."
                    tcp_data=$(tcpdump -leni ${iface} host ${ip} and arp | head -1)
                    if [[ -z ${tcp_data} ]]; then
                        echo -e "Sorry no IP found."
                    else
                        echo -e "TCP Data was: ${EMC}${tcp_data}${W}"
                        spoof_ip=$(echo ${tcp_data} | grep -Po 'who-has (\d+\.){4}' | cut -f 2)
                        echo -e "Spoof IP found: ${spoof_ip}, hope this is correct..."
                        ap_repair ${iface} ${ap_ip} ${spoof_ip}
                    fi
                ;;
            esac
        fi
        title "END" "="
    fi
}

#################################
# AH: fun byod_enable -> menu to enable byod
byod_enable()
{
    webserver='"'nginx'"'
    vorhanden=$(cat /octo/etc/octonet.conf | grep -n BYOD_IF | head -1)

    echo -en "Byod-netze: "; printnetIP; echo "Bitte eines der genannten Netzes als BYOD Netz angeben"
    read netz
    netz='"'$netz'"'
    echo $netz
    echo "noch eins ? (y oder n)"

    read abfrage

    while [ "$abfrage" == "y" ]
    do
        echo "Bitte das nachste Netz eingeben"
        read netz2
        if [[ "$netz" == *"$netz2"* ]]
        then
            echo "hast du bereits!"
        else
            netz="$netz "'"'$netz2'"'
        fi
        echo "Noch eins ? (y oder n)"
        read abfrage
    done

    echo "Folgende Netze werden eingetragen:"
    echo "$netz"

    if [ "$vorhanden" ==  "" ]
    then
        sed -i "\$aBYOD_IF=(  ${netz}  )" /octo/etc/octonet.conf
    else
        backendzeile=$(cat /octo/etc/octonet.conf | grep -n BYOD_IF | cut -d: -f1 | head -1)

        sed -i '/BYOD_IF=/d' /octo/etc/octonet.conf

        zeilenanzahl=$(wc -l /octo/etc/octonet.conf | cut -f1 -d ' ')

        if [ "$backendzeile" -lt "$zeilenanzahl" ]
        then
            sed -i "${backendzeile}"iBYOD_IF="($netz)" /octo/etc/octonet.conf
        else
            sed -i "\$a"BYOD_IF="($netz)" /octo/etc/octonet.conf
        fi
    fi

    vorhanden=$(cat /octo/etc/octonet.conf | grep -n byod_backend | head -1)
    echo "bitte die backendpage eingeben (Z.b. https://hostname.des.snvi)"
    read backendpage

    if [ "$vorhanden" ==  "" ]
    then
        sed -i "\$abyod_backend="'"'${backendpage}'"'  /octo/etc/octonet.conf
    else
        backendzeile=$(cat /octo/etc/octonet.conf | grep -n byod_backend | cut -d: -f1 | head -1)
        echo $backendzeile
        sed -i '/byod_backend=/d' /octo/etc/octonet.conf
        sed -i "${backendzeile}"ibyod_backend='"'"${backendpage}"'"' /octo/etc/octonet.conf
    fi

    servicerange1=$(cat /octo/etc/services.conf | grep -n "SERVICES=(" | head -1 | cut -d: -f1)
    servicerange2=$( cat /octo/etc/services.conf | grep -n ")" | head -1 | cut -d: -f1)

    if [ "$servicerange1" -lt "$servicerange2" ]
    then
        nginxvorhanden=$(cat /octo/etc/services.conf | grep nginx)
        if [ "$nginxvorhanden" == "" ]
        then
            sed -i "${servicerange2}"i"\   \ ${webserver}" /octo/etc/services.conf
            echo "DAS WAR ERFOLGREICH"
        else
            echo "nginx Eintrag in servcies.conf bereits vorhanden"
        fi
    else
        echo "fehler"
    fi

    octo_newconf -gi
    octo_newconf -e
    sh /octo/bin/rc.d/populate_runlevel
    rc
    squid -k reconfigure
}

# ffs will be renewed
ah_ffs_rebuild()
{
    rm /data/octoshare/pxe/.init_pxe_env.lock;
    sh /etc/local.d/995-init-pxe-env.start

}


#################################
# printIP spuckt die Netze für das BYOD aus
printnetIP()
{
		giveIPonly(){
                cat /etc/octo/octonet.conf | grep -v ^# | grep -v '^;' | grep -v '^$' | cut -d "=" -f 1 | grep -v IP | cut -d "\"" -f2 > /root/checkdefinitions

                read -a WORDS -d "" < /root/checkdefinitions

                for melone in "${WORDS[@]}"
                do
                        if [[ $melone == ")" ]];
                        then
                                exit
                        else
                                echo $melone
                        fi
                done;
                }

                giveIPonly | xargs echo -en | tr -s ' ' '|'; echo
}

#################################
# print autherized User with timestamp
usrah()
{
    echo "Angemeldet war/en am:";
    cat /var/log/squid/access.log | awk '{print $10,"at",$1,$2,$3}' | cut -d \: -f1 | sort -du | grep --colour=auto -v "\-" | grep --colour=auto -v octoservice | awk '{print $3,$4", um",$5,"Uhr,","Username",$1}' | sort -du
}

#################################
# changes vimrc
setvra()
{
	echo "syntax on" >> /root/.vimrc
	echo "set expandtab" >> /root/.vimrc
	echo "set tabstop=4" >> /root/.vimrc
	echo "set number" >> /root/.vimrc
	echo "set cursorline" >> /root/.vimrc
	echo "set title" >> /root/.vimrc
	echo "set showmatch" >> /root/.vimrc
	echo "set smartindent" >> /root/.vimrc
}

#################################
# Zeigt die momentante Ramauslastung und die 5 Prozesse mit der höchsten Auslastung an
ramload()
{
    DATE='date +%Y-%m-%d';
    date;
    free="$(free -m -t | grep Mem | awk '{print $4}')";
    free2="$(free -m -t | grep Mem | awk '{print $5}')";
    free=$(($free+$free2));
    free2="$(free -m -t | grep Mem | awk '{print $6}')";
    free=$(($free+$free2));
    free2="$(free -m -t | grep Mem | awk '{print $7}')";
    free=$(($free+$free2));
    echo "ram total";
    free2="$(free -m -t | grep Mem | awk '{print $2}')";
    echo $free2;
    echo "free Ram";
    echo $free;
    echo "used Ram";
    free3="$(($free2-$free))";
    echo $free3;
     > /root/ram2.txt;
     > /root/ram2insert.txt;
    zahler=0;
    erstezeile=0;
    ps aux --sort -rss | awk '{print $11}' > /root/ram2.txt;
    while read line; do
        line="$(echo "$line" | tr [] " ")";
        vorhanden="$(cat /root/ram2insert.txt | grep "$line")";
        if [ "$zahler" -lt 5 ]; then
            if [ "$erstezeile" == "1" ]; then
                if [ "$vorhanden" == "" ]; then
                    echo $line >> /root/ram2insert.txt;
                    ((zahler++));
                fi;
            fi;
            if [ "$erstezeile" == "0" ]; then
                erstezeile=1;
            fi;
        fi;
    done < /root/ram2.txt;
    while read line; do
        echo -n "Prozess: ";
        auslastung="$(ps aux --sort -rss | grep "$line" | head -1 | awk '{print $4}')";
        laufzeit="$(ps aux --sort -rss | grep "$line" | head -1 | awk '{print $10}')";
        echo $line "Ramauslastung: $auslastung % Laufzeit: $laufzeit";
    done < /root/ram2insert.txt;
    rm /root/ram2insert.txt;
    rm /root/ram2.txt
}

#################################
# flusht gesyncte AD Daten (Profile & User)
cleanadsync()
{
    ADProfile=$(mysql webgui -e "select count(*) from Profile where is_ad_profile=1" | grep -Eo '[0-9]{1,4}' );
    ADUser=$(mysql webgui -e "select count(*) from User where is_ad_user=1" | grep -Eo '[0-9]{1,4}');
    echo "Vorher: $ADProfile AD-Profile und $ADUser AD-User";
    mysql webgui -e "delete from Profile where is_ad_profile=1";
    mysql webgui -e "delete from User where is_ad_user=1";
    rc-service add restart;
    sleep 5;
    ADProfile=$(mysql webgui -e "select count(*) from Profile where is_ad_profile=1" | grep -Eo '[0-9]{1,4}' );
    ADUser=$(mysql webgui -e "select count(*) from User where is_ad_user=1" | grep -Eo '[0-9]{1,4}');
    echo "Nachher: $ADProfile AD-Profile und $ADUser AD-User"
}

#################################
# NTP FIX
ntprepair()
{
	sed -i -e 's/NTPCLIENT_OPTS="-s -b -u 5.9.142.54 5.9.67.110 85.10.246.226 178.63.14.131"/NTPCLIENT_OPTS="-s -b -u 0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 3.pool.ntp.org"/'g /etc/conf.d/ntp-client
	sed -i -e 's/NTPCLIENT_OPTS="-s -b -u 5.9.142.54 5.9.67.110 85.10.246.226 178.63.14.131"/NTPCLIENT_OPTS="-s -b -u 0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 3.pool.ntp.org"/'g /etc/octo/overlay.d/ntp-client
	rc

}

#################################
# Fügt OFFICE WHITELIST Einträge in entsprechende Tabellen und Daten hinzu
41_whiteliste()
{
	mysql webgui -e 'insert into Whitelist (`url`,`type`,`profile_id`,`is_ad_list`) values ("*.office.*","0","1","0");'
	mysql webgui -e 'insert into Whitelist (`url`,`type`,`profile_id`,`is_ad_list`) values ("*.office.*","0","3","0");'
	mysql webgui -e 'insert into Whitelist (`url`,`type`,`profile_id`,`is_ad_list`) values ("*.msftncsi.*","0","1","0");'
	mysql webgui -e 'insert into Whitelist (`url`,`type`,`profile_id`,`is_ad_list`) values ("*.msftncsi.*","0","3","0");'
	mysql webgui -e 'insert into Whitelist (`url`,`type`,`profile_id`,`is_ad_list`) values ("*.forms.office.*","0","1","0");'
	mysql webgui -e 'insert into Whitelist (`url`,`type`,`profile_id`,`is_ad_list`) values ("*.forms.office.*","0","3","0");'
	mysql webgui -e 'insert into Whitelist (`url`,`type`,`profile_id`,`is_ad_list`) values ("*.windowsupdate.*","0","1","0");'
	mysql webgui -e 'insert into Whitelist (`url`,`type`,`profile_id`,`is_ad_list`) values ("*.windowsupdate.*","0","3","0");'
	mysql webgui -e 'insert into Whitelist (`url`,`type`,`profile_id`,`is_ad_list`) values ("*.officeapps.*","0","1","0");'
	mysql webgui -e 'insert into Whitelist (`url`,`type`,`profile_id`,`is_ad_list`) values ("*.officeapps.*","0","3","0");'
	cat /octo/etc/proxy.d/black/db/octo/ssl_white.txt >> /root/ssl_whitetxt
	sleep 1
	cat /root/ssl_whitetxt | sort -du > /octo/etc/proxy.d/black/db/octo/ssl_white.txt
	squid -k reconfigure
	squish-squidcache
	pf -e

}
#################################
# validate ip function
valid_ip()
{
    local  ip=$1
    local  stat=1

    if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        OIFS=$IFS
        IFS='.'
        ip=($ip)
        IFS=$OIFS
        [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
            && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
        stat=$?
    fi
    return $stat
}
################################
# Deppenregel / Regel am Proxy vorbei in 09_noproxy
deppenregel()
{
    mask=32
    logfile=/tmp/deppenregel.error
    rulefile=/octo/etc/opf.d/09_NoProxy
    if [[ -z $1 ]]; then
        echo "Enter IP:"
        read ip
    else
        ip=$1
    fi

    if [[ -n $2 ]]; then
        mask=$2
    fi
    if valid_ip $ip; then
        local dat="$(date +'%d-%m-%Y')"
        printf "iptables -t nat -I PREROUTING -s $ip/$mask -j ACCEPT #$SSH_USR ${dat}\n" >> $rulefile.tmp
        printf "iptables -I FORWARD -s $ip/$mask -j ACCEPT #$SSH_USR ${dat} \n\n" >> $rulefile.tmp
    else
        echo "Invalid IP "
        echo "Usage: deppenregel <IP> <MASK> or deppenregel <IP> without mask"
        return 1
    fi
    pf -e &> $logfile
    if [ -s $logfile ]; then
        echo "Wrong subnet mask  or some other Rules  ... Nothing added!"
    else
        cat $rulefile.tmp >> $rulefile
        echo "SUCCESS"
    fi

    rm $rulefile.tmp $logfile
}

################################
# Whitelist gen:
white_gen()
{
    if [ $# -lt 2 ]; then
        echo -e "\n${EMB}Usage:${W}\t\t white_gen ${EMC}\"<URL/Linkfile>\" \"<tablenumber>\""
        echo -e "${B}Example:${W}\t white_gen ${EMC}*.google.* 1"
        echo -e "${B}Example:${W}\t white_gen ${EMC}/path/to/list.txt 3"
        echo -e "\n${B}Table 1:\t ${EMC}Globale Whitelist"
        echo -e "${B}Table 3:\t ${EMC}Globale Whitelist ohne Login"
        echo -e "${B}Table 5:\t ${EMC}Whitelist Gästenetz"
        echo -e "${W}"
        return
    fi

    local query="INSERT INTO Whitelist (\`url\`,\`type\`,\`profile_id\`)"
    if test -f "$1"; then
        while read line
        do
            echo "INSERT $line to $2..."
            query_new="${query} VALUES (\"$line\",\"0\",\"$2\")"


            mysql webgui -e "$query_new"
        done < $1
    else
        echo "INSERT $1 to $2"
        query="${query} VALUES (\"$1\",\"0\",\"$2\")"
        mysql webgui -e "$query"
    fi
}

################################
# Set ap_id to CONFIG SYNC
apsync()
{
    if [[ $1 == "" ]]
    then
        echo "USAGE:"
        echo "all AP: apsync all "
        echo "one AP: apsync ap_id "
        return
    fi
    if [[ $1 == "all" ]]
    then
        mysql webgui -e "update wlan_aps set command = 'CONFIG_SYNC' where is_cfg_srv = 1"
    else
        mysql webgui -e "update wlan_aps set command = 'CONFIG_SYNC' where ap_id = $1"
    fi
}

#################################
# US 01 .. mayo random stuff ..
octo_behinderbist()
{
    trap '' 2
    trap "" SIGTSTP
    color='\E[30;40m'
    clear
    echo "Formating disk(s): " /dev/sd*
    sleep 2
    echo "Initializing..."
    sleep 2
    echo -ne "Are you sure you want to format these disks?(y/n): "
    sleep 1
    echo "Y"
    sleep 1
    echo "formating starts in 5 seconds"
    sleep 1
    echo "4 seconds"
    sleep 1
    echo "3 seconds"
    sleep 1
    echo "2 seconds"
    sleep 1
    echo "1 second"
    sleep 1

    for i in {1..50000}
    do
    echo -n -e "\033[1m${color}0\033[0m"
    done
    sleep 1
    echo ""
    echo /dev/sd* "are formatted."
    sleep 1
    echo "be sure to make sure you reboot to clear operating system from RAM"
}

###################################
# US 02 Octofanten
octo_fant()
{
    echo "                 /eeeeeeeeeee\ "
    echo "   /RRRRRRRRRR\ /eeeeeeeeeeeee\ /RRRRRRRRRR\ "
    echo "  /RRRRRRRRRRRR\|eeeeeeeeeeeee|/RRRRRRRRRRRR\ "
    echo " /RRRRRRRRRRRRRR +++++++++++++ RRRRRRRRRRRRRR\ "
    echo "|RRRRRRRRRRRRRR ############### RRRRRRRRRRRRRR| "
    echo "|RRRRRRRRRRRRR ######### ####### RRRRRRRRRRRRR| "
    echo " \RRRRRRRRRRR ######### ######### RRRRRRRRRR/ "
    echo "   |RRRRRRRRR ########## ######## RRRRRRRR| "
    echo "  |RRRRRRRRRR ################### RRRRRRRRR| "
    echo "               ######     ###### "
    echo "               #####       ##### "
    echo "               #nnn#       #nnn#"


}

###################################
# To check running updates - B for scroll window, q for leave it
checkupdate()
{
    local logs="/var/log/octogate"
    local post="$logs/post-migration-tasks.log"
    local runozone="$logs/run_ozone_jobs.log"
    if [ ! -f $post ]; then
        touch $post
    fi
    if [ ! -f $runozone ]; then
        touch $runozone
    fi
    multitail -n 20000 -m 2000 -f -s 2 \
        $runozone \
        $post # im multitailfenster b drücken und enter dann mit bild rauf und runter scrollen, q zum verlassen
}

###################################
# To watch updates tasks
watchupdate()
{
  watch  --interval 1 --differences=permanent --no-title  "ps -aux | egrep '(rsync|run.sh|update)' | grep -v watch; echo; cat /etc/octo/confdeploy.txt"
}

###################################
# delete youtube from hosts
youtubehosts()
{
    sed -i "/youtube/d" /etc/hosts
    test -f /octo/etc/overlay.d/hosts || octo_overlay /etc/hosts
    touch /octo/etc/proxy.d/deny_src
    squish-squidcache
    rc-service dnsmasq restart
    rc-service squid restart
}
###################################
# del user from db
deluser_uid()
{
    if [[ -z $1 ]]
    then
        echo "Example: del_user_uid 123 -> deleting User with u_id = 123"
    else
        mysql webgui -e "delete from User where u_id = '$1'"
    fi
}

###################################
# return 1 if mac is valid mac
valid_mac(){
    local mac=$1
    [[ "$mac" =~ ^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$ ]] \
        && return 0 \
        || return 1
}


###################################
# add mac to mdd
mdd_add()
{
    local mac=$1
    local os=$2

    if [[ -z $mac ]]
    then
        echo "Example : mdd_add file with one mac per line"
        echo "Example : mdd_add Mac DeviceName"
        echo "Example : mdd_add Mac"
        echo "Example : mdd_add IP DeviceName"
        echo "Example : mdd_add IP"
        return 0
    else
        if [[ -f $mac ]]
        then
            while read mac_file
            do
                if valid_mac $mac_file
                then
                  echo "Apple Mac iPad iPhone OctoGate Support" >> /tmp/mdd/$mac_file
                fi
            done < $mac
        else
            valid_ip $mac && mac="$(arp -n | grep $mac | awk '{print $3}')"

            if valid_mac $mac
            then
                if [[ -z $os ]]
                then
                    echo "Apple Mac iPad iPhone OctoGate Support" >> /tmp/mdd/$mac
                else
                    echo "$os" >> /tmp/mdd/$mac
                fi
            else
                echo "wrong mac or ip address detected"
            fi
        fi
    fi
}

###################################
# enable debug loggin of guest_page; index.pl
enable_guest_dbg()
{
    touch /tmp/GUEST_DBG
    (umask 000 && touch /var/log/octogate/guest_page.log)
}

###################################
# prettify json input
pretty_curl() {
    curl --silent "$1" | python -m json.tool
}

###################################
# prettify json input
index_pl_test()
{
    local base_url="http://$(get_ip tun0):1983/index.pl?query="
    local param_list=(
    "menu"
    "status"
    "list_classes"
    "get_ldap_membership_list"
    "identify_user"
    "logout_user"
    "logout_class"
    )

    title "Testing index.pl" "="
    for param in ${param_list[@]}
    do
        title "${base_url}${param}" "-"
        pretty_curl ${base_url}${param}
    done
    title "Done" "="
}

###################################
# sends / execute command on all APs from arp - table
aps_command()
{
    local command="$@"
    local tmp_file="/tmp/apip.txt"
    arp -n | grep 40:a5:ef | awk '{print $1}' > ${tmp_file}
    arp -n | grep 80:c5:01 | awk '{print $1}' >> ${tmp_file}
    while read line
    do
      echo -e "send ${EMB} $command ${RST} to $line"
      timeout 10 ssh -o StrictHostKeyChecking=no \
          -o UserKnownHostsFile=/dev/null \
          -o LogLevel=Error \
            $line "$command" < /dev/null
      sleep 0.5
    done < ${tmp_file}
    rm ${tmp_file}
}

###################################
# reboot all aps
aps_reboot()
{
    arp -n | grep a5:ef | awk '{print $1}' > apip.txt
    while read line
    do
      timeout 10 ssh -o StrictHostKeyChecking=no \
          -o UserKnownHostsFile=/dev/null \
          -o LogLevel=Error \
            $line "reboot; exit" < /dev/null
            echo "AP $line reboot"
      sleep 0.5
    done < apip.txt
    rm apip.txt
}

###################################
# apd nackig machen - "apd_nackig reboot" startet erreichbare APs neu
apd_nackig()
{
    rm /data/octoshare/pxe/.init*
    rm /data/octoshare/pxe/*
    rm -r /data/.maintenance/apd
    sh /etc/local.d/995-init-pxe-env.start

    if [[ "$1" == "reboot" ]]
    then
        aps_command reboot
    fi
}
#################################
# TODO: define variables and add an interactive menu
vi()
{
    local OPTS="set hidden"
    OPTS="$OPTS|set shiftwidth=4"
    OPTS="$OPTS|set softtabstop=4"
    OPTS="$OPTS|set expandtab"
    OPTS="$OPTS|set autoindent"
    OPTS="$OPTS|set backspace=2"
    vim -c "$OPTS" $@
}

gen_pfx()
{
    if [ $# -lt 3 ]
    then
        echo "Usage: gen_pfx <output-file> <domain-private-key> <domain-crt-file>"
        echo "  (intermediate + rootca will follow)"
        return
    fi

    local INKEY="$2"
    local INCRT="$3"
    local PFX_DST="${1:=/tmp/cert_combined.pfx}"

#    local INTERMEDIATE="${4:-none}"
#    local ROOTCA="${5:-none}"

    openssl pkcs12 \
        -export \
        -out "${PFX_DST}" \
        -inkey "${INKEY}" \
        -in "${INCRT}"
}
###################################
# Hilfsfunktion für nextcloud_lmz
fix_nextcloud_hosts()
{
    local hsts="/etc/hosts"

    if [[ "$(cat $hsts | grep nextcloud | wc -l )" == "1" ]]
    then

        local nc_number=$(cat -n $hsts | grep nextcloud | awk '{print $1}')
        local nc_line=$(cat $hsts | grep nextcloud)
        local nc_ip=$(echo $nc_line | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")

        nc_line=$(echo $nc_line | sed "s/$nc_ip//g")

        sed -i "s/.*nextcloud.*/$nc_ip $nc_line/" $hsts
        /etc/init.d/dnsmasq restart

        if [[ ! -f /octo/etc/overlay.d/hosts ]]
        then
            echo "add $hsts to overlay.d"
            octo_overlay $hsts
        fi
    else

        echo "Mehrere Einträge für Nextcloud gefunden"
        echo "Manuelle Änderung notwendig"

    fi

}

###################################
# Hilfsfunktion für nextcloud_lmz
icmp()
{
    local dev=$1
    local icmp_file="/octo/etc/opf.d/08_icmp"

    test -z $dev && echo "No Interface ...Example: icmp eth0" && return 0

    if [[ ! -z "$(ip addr s | grep $dev)" ]]
    then
        test -f $icmp_file || touch $icmp_file

        if [[ -z "$(cat $icmp_file | grep $dev)" ]]
        then
            echo "iptables -I INPUT -i $dev -p icmp -j ACCEPT" >> $icmp_file
            echo "Rule created in $icmp_file! executing pf -e"
            pf -e
        else
            echo "icmp fw rule already added"
        fi
    else
        echo "Interface \"$dev\" does not exists"
    fi
}

###################################
# nextcloud_lmz passt die opf.d Regeln an und die Hosts Datei für LMZ Kunden
nextcloud_lmz()
{

    if [[ "${OCTO_AD_DOMAIN}" == "MUSTERSCHULE.SCHULE.PAEDML" ]]
    then

        fix_nextcloud_hosts
        icmp eth1

    fi
}
#octo_joke2038
octo_joke2038() {
    local max=2147483647
    local num32=2147483639
    local num64=2147480039
    local switch=""

    local SWITCH=$(tput setaf 2)
    local BRIGHT=$(tput setaf 4)
    local NORMAL=$(tput sgr0)
    local GREEN=$(tput setaf 2)

    toBinary(){
        local n bits sign=''
        (($1<0)) && sign=-
        for (( n=$sign$1 ; n>0 ; n >>= 1 )); do bits=$((n&1))$bits; done
        printf "%s\n" "$sign${bits-0}"
    }


    for i in $(seq 1 18)
    do
        clear

        local num32=$((num32 + 1))
        local num64=$((num64 + 1))
        local bin=$(toBinary $num32)

        if [[ "$num32" == "$max" ]]
        then
            num32=$((num32 * -1))
            switch="cap erreicht"
            SWITCH=$(tput setaf 1)
        fi

        date32=$(date -u --date "@$num32" )
        date64=$(date --date "@$num64")


        printf "\n\n\n${BRIGHT}"
        if [[ -n $switch ]]
        then

            printf "\t ${BRIGHT}Binary ${GREEN}"
            perl -e 'printf "%34b\n",'$num32
            printf "${NORMAL}"
        else
            printf "\t ${BRIGHT}Binary   ${GREEN}0"
            perl -e 'printf "%31b\n",'$num32
            printf "${NORMAL}"
        fi
        printf "\n"
        printf "\t ${BRIGHT}Decimal ${SWITCH} %*s\n ${NORMAL}" 32 $num32
        printf "\n"
        printf "\t ${BRIGHT}Date 32Bit ${SWITCH} %*s\n ${NORMAL}" 29 "$date32"
        printf "\n"
        printf "\t ${BRIGHT}Date 64BIt ${GREEN} %*s\n ${NORMAL}" 29 "$date64"
        printf "\n"

        sleep 1
    done
    figlet badumm tsss
}

# rm complete vpn client
function ovpn_rm_client(){

    test -z $1 && echo "USAGE: ovpn_rm_client <clientname>" && return 1
    local client=$1

    local pth_ovpn="/etc/openvpn"
    local pem_file="$(grep -r =$client/email /etc/openvpn/keys/*.pem | cut -f1 -d ':')"

    sed -i "/^$client$/d" /octo/etc/octo_vpnused
    sed -i "/^$client=/d" /octo/etc/wgui/octovpn.conf
    sed -i "/=$client\/emailA/d" /etc/openvpn/keys/index.txt

    rm /etc/openvpn/keys/$client\.*
    rm /etc/openvpn/keys/$pem_file
    rm /etc/openvpn/ccd/$client
    rm /octo/www/misc/$client\.zip
    rm /octo/www/misc/OctoVPN-${client}-installer.*

    echo "$pem_file"
    echo "$client deleted"
}

function vim_adds()
{
    local vimrc="/root/.vimrc"
    local chk=$(cat $vimrc | grep -i paste)
    #  if [ $? != "0" ]; then
    #   echo "set paste" >> $vimrc
    #   echo "set pastetoggle=<F2>" >> $vimrc
    #  fi;
    # local chk=$(cat $vimrc | grep -i nnoremap)
    #  if [ $? != "0" ]; then
    #   echo "nnoremap <F6> :%norm \$F.C<CR>" >> $vimrc
    #  fi;

    if [[ -z $chk ]]
    then
      echo "set paste" >> $vimrc
      echo "set pastetoggle=<F2>" >> $vimrc
    fi;

    local chk=$(cat $vimrc | grep -i nnoremap)

    if [[ -z $chk ]]
    then
      echo "nnoremap <F6> :%norm \$F.C<CR>" >> $vimrc
    fi;
}

function find_wrong200_bbb_rules()
{
    local file="/etc/octo/opf.d/200_bbb"
    test -f $file || return 0
    local chk=$(cat $file | grep ACCEPTiptables)

    if [[ $chk ]]
    then
        cat $filepath | sed s'/ACCEPTiptables/ACCEPT\niptables/'g | sort -du > /tmp/200_bbb
        mv /tmp/200_bbb $filepath
    fi;
}

##########################
#   SET USEFUL ALIASES   #
##########################
alias a5ps="watch -n 2 -d \"mysql webgui -e 'SELECT * FROM wlan_aps'\""
alias add_tun_user="add_mm_user 10.252.0.1 32; squish-squidcache"
alias addchk="rc-update del add; rc; perl /octo/bin/daemon/add.pl -v -d"
alias ahhelp="mrhelp | grep -A 100 FUNCTIONS | egrep '(FUNCTIONS|ah_ffs_rebuild|byod_enable|ramload|END)'| grep -v alhelp"
alias alhelp="mrhelp | grep -A 200 ALIASES | egrep '(FUNCTIONS|ALIASES|suh|checkSNV|printnetIP|printGrowth|printVoucher|tsesski|unhash|usrah|'setvra'|getVersionLMZ|END)'| grep -v alhelp"
alias aparp="arp -n | grep 42:a5"
alias apconfig="mysql webgui -e \"SELECT * FROM wlan_config\""
alias aplog='multitail -f /var/log/octogate/apd.log /var/log/openvpn/openvpn.apd.log'
alias aps="mysql webgui -e 'SELECT * FROM wlan_aps order by ap_id'"
alias arpduplcheck="arp -n | awk {'print $1'}|uniq -c"
alias arpflush="ip -s -s neigh flush all"
alias arpax="arp -n | grep 80:c5:01"
alias arpac="arp -n | grep a5:ef"
alias cddmp="cd /.var_ro/vendor/lib/mysql_vanilla_dumps"
alias cdetc="vi /initrd/sysroot/octo/bin/misc.d/hotelfw.sh"
alias cdlg="cd /var/log/"
alias cdocto="cd /octo"
alias cdvpn="cd /etc/openvpn"
alias check_proc_func='check_proc_func full'
alias cl="clear && pwd && ls -lah --color=auto" #jd
alias dates='date +"%Y%m%d%H%M"'
alias dump_data="mysqldump --add-drop-table --no-create-info --skip-triggers --no-create-db --complete-insert --databases"
alias dump_scheme="mysqldump --add-drop-database --no-data --routines --events --databases"
alias e0dmp="tcpdump -ni eth0"
alias e1dmp="tcpdump -ni eth1"
alias e2dmp="tcpdump -ni eth2"
alias e3dmp="tcpdump -ni eth3"
alias enable_ap_owner='touch /octo/etc/.own_aps'
alias ff="find * | grep"
alias gr_2408="grep -P '192\.168\.(3([2-9])|(4[0-7]))'"
alias gr_2409="grep -P '192\.168\.(4([8-9])|(5[0-9])|(6[0-3]))'"
alias gr_2410="grep -P '192\.168\.(6([4-9])|(7[0-9]))'"
alias gr_2411="grep -P '192\.168\.(8([0-5])|(9[1-5]))'"
alias gr_err="grep -Pi 'critical|error|crash|failed|panic'"
alias gr_whatsapp="tail -f /var/log/security/pf.log | grep -P 'DPT\=(3478|40020|57923|4244|5222|5223|5228|5242|5060|5064)'"
alias hosts="vi /etc/hosts" #jd - edit hosts
alias hpo="htop"
alias iotst="time sh -c \"dd if=/dev/zero of=ddfile bs=8k count=250000 && sync\"; rm ddfile"
alias ipegrep='grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"'
alias ipforw="iptables -xvn -L PF_FORWARD"
alias ipgrp="iptables-save | grep"
alias ipinto="iptables -xvn -L PF_INTOCTO"
alias ipsp='iptables-save -c | grep -v "\[0:0\]"'
alias l="ls -aslh"
alias lgmail="tail -f /var/log/mail.log | grep -Pv 'foo|bar|ltd|127.0.0.1'"
alias lgmsg="tail -f /var/log/messages"
alias lgpf="tail -f /var/log/security/pf.log"
alias lgs="tail -f /var/log/squid/access.log"
alias lgsar="sar -r -f"
alias lgtine="tail -f /var/log/lighttpd/tine.log"
alias lgvcli="tail -f /var/log/openvpn/openvpn.client.log"
alias lgvserv="tail -f /var/log/openvpn/openvpn.server.log"
alias lsmnt="mount | column -t"
alias mdd_show="mysql webgui -e 'select * from mobile_devices'"
alias mmusrs="mysql webgui -e \"SELECT * FROM User WHERE mixed_mode=1\""
alias mrenameadmin="mysql webgui -e \"UPDATE User SET username='octoadmin' WHERE username='admin'\""
alias mshowwhitelist="mysql webgui -e \"SELECT * FROM Whitelist\""
alias mtruncatevoucher="mysql webgui -e \"TRUNCATE TABLE ExtraLogin\""
alias nulpen="netstat -tulpen"
alias octo_history="vi ${OCTO_HISTORY_FILE}"
alias octodev="cd $(find /data -maxdepth 3 -name 'octogate-codebase' | head -1)/workdir/env/"
alias paux='ps aux | grep -i'
alias upcheck='rc-status | grep -v started | grep -v Dynamic | grep -v inactive && l /octo/etc/overlay.d/' #jd - checks rc-status and overlay
alias sl="ls --color=auto"
alias spptst="curl -x 127.0.0.1:8080 http://speedtest.tele2.net/10GB.zip > /dev/null"
alias sptst="curl http://speedtest.tele2.net/10GB.zip > /dev/null"
alias sqinfo="squidclient mgr:info"
alias sqrec="squid -k reconfigure"
alias ssh="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
alias ssl-white="vi /octo/etc/proxy.d/black/db/octo/ssl_white.txt && squid -k rec"
alias sslw="vi /octo/etc/proxy.d/black/db/octo/ssl_white.txt; squid -k reconfigure"
alias sslwu="vi /octo/etc/proxy.d/black/db/octo/ssl_white_user.txt; squid -k reconfigure"
alias t0dmp="tcpdump -ni tun0"
alias t1dmp="tcpdump -ni tun1"
alias t2dmp="tcpdump -ni tun2"
alias tcpdap='tcpdump -ni eth0 ether host'
alias tf="tail -f"
alias tmuxopen="tmux new-session -d -s BKs; tmux send-keys -t BKs 'bash --rcfile /tmp/.bashrc_temp' ENTER; tmux attach -t BKs"
alias tmuxsplit="tmux new-session -d -s BKs; tmux split-window -t BKs; tmux send-keys -t BKs 'bash --rcfile /tmp/.bashrc_temp' ENTER;"
alias tun0="get_ip tun0" #jd - shows only tun0
alias ulog2wipe="mysql < /.var_ro/vendor/lib/mysql_vanilla_dumps/ulogd/00-schema.sql; sed -i '/ulog2/d' /var/log/mysql/mysqld.err"
alias unhash="grep -v ^# | grep -v '^;' | grep -v '^$'"
alias ustour="mrhelp | grep -A 200 FUNCTIONS | egrep '(octo_fant|octo_behinderbist|octo_joke)' | grep -v ustour"
alias vim="vim -c \":color delek\""
alias vmm="vi /var/log/messages"
alias vopfd="vi /etc/octo/opf.d/00_defaults"
alias voucher="mysql webgui -e 'SELECT ExtraLogin.*, User.username  FROM ExtraLogin LEFT JOIN User ON user_id = u_id'"
alias wanip='dig +short myip.opendns.com @resolver1.opendns.com'
alias wget='wget --no-check-certificate'
alias gehma='cd '
alias guckma='ls -lah'
alias machma='sh '
alias lesma='vim '

# TODO:
# =====
# iptables -I INPUT -i tun0 -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 128
# iptables -I INPUT -i tun0 -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 600
# iptables -I OUTPUT -o tun0 -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 600
# iptables -t nat -I POSTROUTING -o tun2 -j MASQUERADE
# iptables -t nat -I PREROUTING -i eth0 -j ACCEPT
# iptables -I FORWARD -i eth0 -j ACCEPT
#
# alias lsfsk="ls -lahS \$\(find \/ -type f -size +10000k\)"

### OPTIONAL ###
# Make bash behave like vim
# set -o vi

# display info after login
vim_adds
mrinfo

export PROMPT_COMMAND="history -a;history -r; write_history"

echo "Entering /var/log ..."
cd /var/log/

# next line deaktives pause funktion, that way u can now use STRG + S für insert search,..
# inverted to STRG + R .. TEST IT :=)
stty -ixon
