#!/bin/bash
declare -A TIMEUS
SCPdir="/etc/newadm"
SCPusr="${SCPdir}/ger-user"
MyPID="${SCPusr}/pid-adm"
MyTIME="${SCPusr}/time-adm"
USRdatabase="/etc/ADMuser"
VERY="$(ps aux|grep "${SCPusr}/usercodes verificar"| grep -v grep)"
[[ -e ${MyPID} ]] && source ${MyPID} || touch ${MyPID}
[[ -e ${MyTIME} ]] && source ${MyTIME} || touch ${MyTIME}
[[ ! -e ${USRdatabase} ]] && touch ${USRdatabase}
sort ${USRdatabase} | uniq > ${USRdatabase}tmp
mv -f ${USRdatabase}tmp ${USRdatabase}
# Open VPN
newclient () {
#Nome #Senha
usermod -p $(openssl passwd -1 $2) $1
  while [[ ${newfile} != @(s|S|y|Y|n|N) ]]; do
   read -p "$(fun_trans "Criar Arquivo Openvpn")? [S/N]: " -e -i S newfile
   tput cuu1 && tput dl1
  done
if [[ ${newfile} = @(s|S) ]]; then
cp /etc/openvpn/client-common.txt $HOME/$1.ovpn
echo "<key>
$(cat /etc/openvpn/client-key.pem)
</key>
<cert>
$(cat /etc/openvpn/client-cert.pem)
</cert>
<ca>
$(cat /etc/openvpn/ca.pem)
</ca>" >> $HOME/$1.ovpn
  while [[ ${ovpnauth} != @(s|S|y|Y|n|N) ]]; do
    read -p "$(fun_trans "Colocar Autenticacao de Usuario no Arquivo")? [S/N]: " -e -i S ovpnauth
    tput cuu1 && tput dl1
  done
  [[ ${ovpnauth} = @(n|N) ]] && sed -i "s;auth-user-pass;<auth-user-pass>\n$1\n$2\n</auth-user-pass>;g" $HOME/$1.ovpn
  cd $HOME
  zip ./$1.zip ./$1.ovpn > /dev/null 2>&1
  rm ./$1.ovpn > /dev/null 2>&1
  echo -e "\033[1;31m$(fun_trans "Arquivo Criado em"): ($HOME/$1.zip)"
 fi
}
block_userfun () {
local USRloked="/etc/newadm-userlock"
local LIMITERLOG="${USRdatabase}/Limiter.log"
if [[ $2 = "-loked" ]]; then
[[ $(cat ${USRloked}|grep -w "$1") ]] && return 1
echo "USER: $1 (LOKED - MULTILOGUIN) $(date +%r)"
fi
if [[ $(cat ${USRloked}|grep -w "$1") ]]; then
usermod -U "$1" &>/dev/null
[[ -e ${USRloked} ]] && {
   newbase=$(cat ${USRloked}|grep -w -v "$1")
   [[ -e ${USRloked} ]] && rm ${USRloked}
   for value in `echo ${newbase}`; do
   echo $value >> ${USRloked}
   done
   }
[[ -e ${LIMITERLOG} ]] && [[ $(cat ${LIMITERLOG}|grep -w "$1") ]] && {
   newbase=$(cat ${LIMITERLOG}|grep -w -v "$1")
   [[ -e ${LIMITERLOG} ]] && rm ${LIMITERLOG}
   for value in `echo ${newbase}`; do
   echo $value >> ${LIMITERLOG}
   done
}
return 1
else
usermod -L "$1" &>/dev/null
echo $1 >> ${USRloked}
return 0
fi
}
block_user () {
local USRloked="/etc/newadm-userlock"
[[ ! -e ${USRloked} ]] && touch ${USRloked}
usuarios_ativos=($(mostrar_usuarios))
if [[ -z ${usuarios_ativos[@]} ]]; then
msg -verm "$(fun_trans "Nenhum Usuario Cadastrado")"
msg -bar
return 1
else
msg -ama "$(fun_trans "Usuarios Atualmente Ativos no Servidor")"
msg -bar
Numb=0
for us in $(echo ${usuarios_ativos[@]}); do
if [[ $(cat ${USRloked}|grep -w "${us}") ]]; then
msg -ne "[$Numb] ->" && echo -e "\033[1;33m ${us} \033[1;31mLoked"
else
msg -ne "[$Numb] ->" && echo -e "\033[1;33m ${us} \033[1;32mUnlocked"
fi
let Numb++
done
msg -bar
fi
msg -ama "$(fun_trans "Digite ou Selecione um Usuario")"
msg -bar
unset selection
while [[ ${selection} = "" ]]; do
echo -ne "\033[1;37mSelect: " && read selection
tput cuu1 && tput dl1
done
if [[ ! $(echo "${selection}" | egrep '[^0-9]') ]]; then
usuario_del="${usuarios_ativos[$selection]}"
else
usuario_del="$selection"
fi
[[ -z $usuario_del ]] && {
     msg -verm "$(fun_trans "Erro, Usuario Invalido")"
     msg -bar
     return 1
     }
[[ ! $(echo ${usuarios_ativos[@]}|grep -w "$usuario_del") ]] && {
     msg -verm "$(fun_trans "Erro, Usuario Invalido")"
     msg -bar
     return 1
     }
msg -ne "$(fun_trans "Usuario Selecionado"): " && echo -ne "$usuario_del "
block_userfun "$usuario_del" && msg -verm "[$(fun_trans "Bloqueado")]" || msg -verd "[$(fun_trans "Desbloqueado")]"
msg -bar
}
add_user () {
#nome senha Dias limite
[[ $(cat /etc/passwd |grep $1: |grep -vi [a-z]$1 |grep -v [0-9]$1 > /dev/null) ]] && return 1
valid=$(date '+%C%y-%m-%d' -d " +$3 days") && datexp=$(date "+%F" -d " + $3 days")
useradd -M -s /bin/false $1 -e ${valid} > /dev/null 2>&1 || return 1
(echo $2; echo $2)|passwd $1 2>/dev/null || {
    userdel --force $1
    return 1
    }
[[ -e ${USRdatabase} ]] && {
   newbase=$(cat ${USRdatabase}|grep -w -v "$1")
   echo "$1|$2|${datexp}|$4" > ${USRdatabase}
   for value in `echo ${newbase}`; do
   echo $value >> ${USRdatabase}
   done
   } || echo "$1|$2|${datexp}|$4" > ${USRdatabase}
}
renew_user_fun () {
#nome dias
datexp=$(date "+%F" -d " + $2 days") && valid=$(date '+%C%y-%m-%d' -d " + $2 days")
chage -E $valid $1 2> /dev/null || return 1
[[ -e ${USRdatabase} ]] && {
   newbase=$(cat ${USRdatabase}|grep -w -v "$1")
   useredit=$(cat ${USRdatabase}|grep -w "$1")
   pass=$(echo $useredit|cut -d'|' -f2)
   limit=$(echo $useredit|cut -d'|' -f4)
   echo "$1|$pass|${datexp}|$limit" > ${USRdatabase}
   for value in `echo ${newbase}`; do
   echo $value >> ${USRdatabase}
   done
   }
}
edit_user_fun () {
#nome senha dias limite
(echo "$2" ; echo "$2" ) |passwd $1 > /dev/null 2>&1 || return 1
datexp=$(date "+%F" -d " + $3 days") && valid=$(date '+%C%y-%m-%d' -d " + $3 days")
chage -E $valid $1 2> /dev/null || return 1
[[ -e ${USRdatabase} ]] && {
   newbase=$(cat ${USRdatabase}|grep -w -v "$1")
   echo "$1|$2|${datexp}|$4" > ${USRdatabase}
   for value in `echo ${newbase}`; do
   echo $value >> ${USRdatabase}
   done
   } || echo "$1|$2|${datexp}|$4" > ${USRdatabase}
}
rm_user () {
#nome
userdel --force "$1" &>/dev/null || return 1
[[ -e ${USRdatabase} ]] && {
   newbase=$(cat ${USRdatabase}|grep -w -v "$1")
   for value in `echo ${newbase}`; do
   echo $value >> ${USRdatabase}
   done
   }
}
mostrar_usuarios () {
for u in `awk -F : '$3 > 900 { print $1 }' /etc/passwd | grep -v "nobody" |grep -vi polkitd |grep -vi system-`; do
echo "$u"
done
}
dropbear_pids () {
local pids
local port_dropbear=`ps aux | grep dropbear | awk NR==1 | awk '{print $17;}'`
cat /var/log/auth.log|grep "$(date|cut -d' ' -f2,3)" > /var/log/authday.log
# cat /var/log/auth.log|tail -1000 > /var/log/authday.log
local log=/var/log/authday.log
local loginsukses='Password auth succeeded'
[[ -z $port_dropbear ]] && return 1
for port in `echo $port_dropbear`; do
 for pidx in $(ps ax |grep dropbear |grep "$port" |awk -F" " '{print $1}'); do
  pids="${pids}$pidx\n"
 done
done
for pid in `echo -e "$pids"`; do
  pidlogs=`grep $pid $log |grep "$loginsukses" |awk -F" " '{print $3}'`
  i=0
    for pidend in $pidlogs; do
    let i++
    done
    if [[ $pidend ]]; then
    login=$(grep $pid $log |grep "$pidend" |grep "$loginsukses")
    PID=$pid
    user=`echo $login |awk -F" " '{print $10}' | sed -r "s/'//g"`
    waktu=$(echo $login |awk -F" " '{print $2"-"$1,$3}')
    [[ -z $user ]] && continue
    echo "$user|$PID|$waktu"
    fi
done
}
openvpn_pids () {
#nome|#loguin|#rcv|#snd|#time
  byte () {
   while read B dummy; do
   [[ "$B" -lt 1024 ]] && echo "${B} bytes" && break
   KB=$(((B+512)/1024))
   [[ "$KB" -lt 1024 ]] && echo "${KB} Kb" && break
   MB=$(((KB+512)/1024))
   [[ "$MB" -lt 1024 ]] && echo "${MB} Mb" && break
   GB=$(((MB+512)/1024))
   [[ "$GB" -lt 1024 ]] && echo "${GB} Gb" && break
   echo $(((GB+512)/1024)) terabytes
   done
   }
for user in $(mostrar_usuarios); do
user="$(echo $user|sed -e 's/[^a-z0-9 -]//ig')"
[[ ! $(sed -n "/^${user},/p" /etc/openvpn/openvpn-status.log) ]] && continue
i=0
unset RECIVED; unset SEND; unset HOUR
 while read line; do
 IDLOCAL=$(echo ${line}|cut -d',' -f2)
 RECIVED+="$(echo ${line}|cut -d',' -f3)+"
 SEND+="$(echo ${line}|cut -d',' -f4)+"
 DATESEC=$(date +%s --date="$(echo ${line}|cut -d',' -f5|cut -d' ' -f1,2,3,4)")
 TIMEON="$(($(date +%s)-${DATESEC}))"
  MIN=$(($TIMEON/60)) && SEC=$(($TIMEON-$MIN*60)) && HOR=$(($MIN/60)) && MIN=$(($MIN-$HOR*60))
  HOUR+="${HOR}h:${MIN}m:${SEC}s\n"
  let i++
 done <<< "$(sed -n "/^${user},/p" /etc/openvpn/openvpn-status.log)"
RECIVED=$(echo $(echo ${RECIVED}0|bc)|byte)
SEND=$(echo $(echo ${SEND}0|bc)|byte)
HOUR=$(echo -e $HOUR|sort -n|tail -1)
echo -e "$user|$i|$RECIVED|$SEND|$HOUR"
done
}
err_fun () {
     case $1 in
     1)msg -verm "$(fun_trans "Usuario Nulo")"; sleep 2s; tput cuu1; tput dl1; tput cuu1; tput dl1;;
     2)msg -verm "$(fun_trans "Usuario Com Nome Muito Curto")"; sleep 2s; tput cuu1; tput dl1; tput cuu1; tput dl1;;
     3)msg -verm "$(fun_trans "Usuario Com Nome Muito Grande")"; sleep 2s; tput cuu1; tput dl1; tput cuu1; tput dl1;;
     4)msg -verm "$(fun_trans "Senha Nula")"; sleep 2s; tput cuu1; tput dl1; tput cuu1; tput dl1;;
     5)msg -verm "$(fun_trans "Senha Muito Curta")"; sleep 2s; tput cuu1; tput dl1; tput cuu1; tput dl1;;
     6)msg -verm "$(fun_trans "Senha Muito Grande")"; sleep 2s; tput cuu1; tput dl1; tput cuu1; tput dl1;;
     7)msg -verm "$(fun_trans "Duracao Nula")"; sleep 2s; tput cuu1; tput dl1; tput cuu1; tput dl1;;
     8)msg -verm "$(fun_trans "Duracao invalida utilize numeros")"; sleep 2s; tput cuu1; tput dl1; tput cuu1; tput dl1;;
     9)msg -verm "$(fun_trans "Duracao maxima e de um ano")"; sleep 2s; tput cuu1; tput dl1; tput cuu1; tput dl1;;
     11)msg -verm "$(fun_trans "Limite Nulo")"; sleep 2s; tput cuu1; tput dl1; tput cuu1; tput dl1;;
     12)msg -verm "$(fun_trans "Limite invalido utilize numeros")"; sleep 2s; tput cuu1; tput dl1; tput cuu1; tput dl1;;
     13)msg -verm "$(fun_trans "Limite maximo e de 999")"; sleep 2s; tput cuu1; tput dl1; tput cuu1; tput dl1;;
     14)msg -verm "$(fun_trans "Usuario Ja Existe")"; sleep 2s; tput cuu1; tput dl1; tput cuu1; tput dl1;;
     esac
}
new_user () {
usuarios_ativos=($(mostrar_usuarios))
if [[ -z ${usuarios_ativos[@]} ]]; then
msg -verm "$(fun_trans "Nenhum Usuario Cadastrado")"
msg -bar
else
msg -ama "$(fun_trans "Usuarios Atualmente Ativos no Servidor")"
msg -bar
for us in $(echo ${usuarios_ativos[@]}); do
msg -ne "User: " && echo "${us}"
done
msg -bar
fi
while true; do
     msg -ne "$(fun_trans "Nome Do Novo Usuario")"
     read -p ": " nomeuser
     nomeuser="$(echo $nomeuser|sed -e 's/[^a-z0-9 -]//ig')"
     if [[ -z $nomeuser ]]; then
     err_fun 1 && continue
     elif [[ "${#nomeuser}" -lt "4" ]]; then
     err_fun 2 && continue
     elif [[ "${#nomeuser}" -gt "24" ]]; then
     err_fun 3 && continue
     elif [[ "$(echo ${usuarios_ativos[@]}|grep -w "$nomeuser")" ]]; then
     err_fun 14 && continue
     fi
     break
done
while true; do
     msg -ne "$(fun_trans "Senha Novo Usuario")"
     read -p ": " senhauser
     if [[ -z $senhauser ]]; then
     err_fun 4 && continue
     elif [[ "${#senhauser}" -lt "6" ]]; then
     err_fun 5 && continue
     elif [[ "${#senhauser}" -gt "20" ]]; then
     err_fun 6 && continue
     fi
     break
done
while true; do
     msg -ne "$(fun_trans "Tempo de Duracao do Novo Usuario")"
     read -p ": " diasuser
     if [[ -z "$diasuser" ]]; then
     err_fun 7 && continue
     elif [[ "$diasuser" != +([0-9]) ]]; then
     err_fun 8 && continue
     elif [[ "$diasuser" -gt "360" ]]; then
     err_fun 9 && continue
     fi 
     break
done
while true; do
     msg -ne "$(fun_trans "Limite de Conexao do Novo Usuario")"
     read -p ": " limiteuser
     if [[ -z "$limiteuser" ]]; then
     err_fun 11 && continue
     elif [[ "$limiteuser" != +([0-9]) ]]; then
     err_fun 12 && continue
     elif [[ "$limiteuser" -gt "999" ]]; then
     err_fun 13 && continue
     fi
     break
done
     tput cuu1 && tput dl1
     tput cuu1 && tput dl1
     tput cuu1 && tput dl1
     tput cuu1 && tput dl1
     msg -ne "$(fun_trans "IP do Servidor"): " && echo -e "$(meu_ip)"
     msg -ne "$(fun_trans "Usuario"): " && echo -e "$nomeuser"
     msg -ne "$(fun_trans "Senha"): " && echo -e "$senhauser"
     msg -ne "$(fun_trans "Dias de Duracao"): " && echo -e "$diasuser"
     msg -ne "$(fun_trans "Data de Expiracao"): " && echo -e "$(date "+%F" -d " + $diasuser days")"
     msg -ne "$(fun_trans "Limite de Conexao"): " && echo -e "$limiteuser"
msg -bar
add_user "${nomeuser}" "${senhauser}" "${diasuser}" "${limiteuser}" && msg -ama "$(fun_trans "Usuario Criado Com Sucesso")" || msg -verm "$(fun_trans "Erro, Usuario nao criado")"
[[ $(dpkg --get-selections|grep -w "openvpn"|head -1) ]] && [[ -e /etc/openvpn/openvpn-status.log ]] && newclient "$nomeuser" "$senhauser"
msg -bar
}
remove_user () {
usuarios_ativos=($(mostrar_usuarios))
if [[ -z ${usuarios_ativos[@]} ]]; then
msg -verm "$(fun_trans "Nenhum Usuario Cadastrado")"
msg -bar
return 1
else
msg -ama "$(fun_trans "Usuarios Atualmente Ativos no Servidor")"
msg -bar
i=0
for us in $(echo ${usuarios_ativos[@]}); do
msg -ne "[$i] ->" && echo -e "\033[1;33m ${us}"
let i++
done
msg -bar
fi
msg -ama "$(fun_trans "Digite ou Selecione um Usuario")"
msg -bar
unset selection
while [[ -z ${selection} ]]; do
echo -ne "\033[1;37m$(fun_trans "Selecione A Opcao"): " && read selection
tput cuu1 && tput dl1
done
if [[ ! $(echo "${selection}" | egrep '[^0-9]') ]]; then
usuario_del="${usuarios_ativos[$selection]}"
else
usuario_del="$selection"
fi
[[ -z $usuario_del ]] && {
     msg -verm "$(fun_trans "Erro, Usuario Invalido")"
     msg -bar
     return 1
     }
[[ ! $(echo ${usuarios_ativos[@]}|grep -w "$usuario_del") ]] && {
     msg -verm "$(fun_trans "Erro, Usuario Invalido")"
     msg -bar
     return 1
     }
msg -ne "$(fun_trans "Usuario Selecionado"): " && echo -ne "$usuario_del"
rm_user "$usuario_del" && msg -verd " [$(fun_trans "Removido")]" || msg -verm " [$(fun_trans "Nao Removido")]"
msg -bar
}
renew_user () {
usuarios_ativos=($(mostrar_usuarios))
if [[ -z ${usuarios_ativos[@]} ]]; then
msg -verm "$(fun_trans "Nenhum Usuario Cadastrado")"
msg -bar
return 1
else
msg -ama "$(fun_trans "Usuarios Atualmente Ativos no Servidor")"
msg -bar
i=0
for us in $(echo ${usuarios_ativos[@]}); do
msg -ne "[$i] ->" && echo -e "\033[1;33m ${us}"
let i++
done
msg -bar
fi
msg -ama "$(fun_trans "Digite ou Selecione um Usuario")"
msg -bar
unset selection
while [[ -z ${selection} ]]; do
echo -ne "\033[1;37m$(fun_trans "Selecione A Opcao"): " && read selection
tput cuu1
tput dl1
done
if [[ ! $(echo "${selection}" | egrep '[^0-9]') ]]; then
useredit="${usuarios_ativos[$selection]}"
else
useredit="$selection"
fi
[[ -z $useredit ]] && {
     msg -verm "$(fun_trans "Erro, Usuario Invalido")"
     msg -bar
     return 1
     }
[[ ! $(echo ${usuarios_ativos[@]}|grep -w "$useredit") ]] && {
     msg -verm "$(fun_trans "Erro, Usuario Invalido")"
     msg -bar
     return 1
     }
while true; do
     msg -ne "$(fun_trans "Novo Tempo de Duracao de"): $useredit"
     read -p ": " diasuser
     if [[ -z "$diasuser" ]]; then
     echo -e '\n\n\n'
     err_fun 7 && continue
     elif [[ "$diasuser" != +([0-9]) ]]; then
     echo -e '\n\n\n'
     err_fun 8 && continue
     elif [[ "$diasuser" -gt "360" ]]; then
     echo -e '\n\n\n'
     err_fun 9 && continue
     fi
     break
done
msg -bar
renew_user_fun "${useredit}" "${diasuser}" && msg -ama "$(fun_trans "Usuario Modificado Com Sucesso")" || msg -verm "$(fun_trans "Erro, Usuario nao Modificado")"
msg -bar
}
edit_user () {
usuarios_ativos=($(mostrar_usuarios))
if [[ -z ${usuarios_ativos[@]} ]]; then
msg -verm "$(fun_trans "Nenhum Usuario Cadastrado")"
msg -bar
return 1
else
msg -ama "$(fun_trans "Usuarios Atualmente Ativos no Servidor")"
msg -bar
i=0
for us in $(echo ${usuarios_ativos[@]}); do
msg -ne "[$i] ->" && echo -e "\033[1;33m ${us}"
let i++
done
msg -bar
fi
msg -ama "$(fun_trans "Digite ou Selecione um Usuario")"
msg -bar
unset selection
while [[ -z ${selection} ]]; do
echo -ne "\033[1;37m$(fun_trans "Selecione A Opcao"): " && read selection
tput cuu1; tput dl1
done
if [[ ! $(echo "${selection}" | egrep '[^0-9]') ]]; then
useredit="${usuarios_ativos[$selection]}"
else
useredit="$selection"
fi
[[ -z $useredit ]] && {
     msg -verm "$(fun_trans "Erro, Usuario Invalido")"
     msg -bar
     return 1
     }
[[ ! $(echo ${usuarios_ativos[@]}|grep -w "$useredit") ]] && {
     msg -verm "$(fun_trans "Erro, Usuario Invalido")"
     msg -bar
     return 1
     }
while true; do
msg -ne "$(fun_trans "Usuario Selecionado"): " && echo -e "$useredit"
     msg -ne "$(fun_trans "Nova Senha de"): $useredit"
     read -p ": " senhauser
     if [[ -z "$senhauser" ]]; then
     err_fun 4 && continue
     elif [[ "${#senhauser}" -lt "6" ]]; then
     err_fun 5 && continue
     elif [[ "${#senhauser}" -gt "20" ]]; then
     err_fun 6 && continue
     fi
     break
done
while true; do
     msg -ne "$(fun_trans "Dias de Duracao de"): $useredit"
     read -p ": " diasuser
     if [[ -z "$diasuser" ]]; then
     err_fun 7 && continue
     elif [[ "$diasuser" != +([0-9]) ]]; then
     err_fun 8 && continue
     elif [[ "$diasuser" -gt "360" ]]; then
     err_fun 9 && continue
     fi
     break
done
while true; do
     msg -ne "$(fun_trans "Novo Limite de Conexao de"): $useredit"
     read -p ": " limiteuser
     if [[ -z "$limiteuser" ]]; then
     err_fun 11 && continue
     elif [[ "$limiteuser" != +([0-9]) ]]; then
     err_fun 12 && continue
     elif [[ "$limiteuser" -gt "999" ]]; then
     err_fun 13 && continue
     fi
     break
done
     tput cuu1 && tput dl1
     tput cuu1 && tput dl1
     tput cuu1 && tput dl1
     tput cuu1 && tput dl1
     msg -ne "$(fun_trans "Usuario"): " && echo -e "$useredit"
     msg -ne "$(fun_trans "Senha"): " && echo -e "$senhauser"
     msg -ne "$(fun_trans "Dias de Duracao"): " && echo -e "$diasuser"
     msg -ne "$(fun_trans "Data de Expiracao"): " && echo -e "$(date "+%F" -d " + $diasuser days")"
     msg -ne "$(fun_trans "Limite de Conexao"): " && echo -e "$limiteuser"
msg -bar
edit_user_fun "${useredit}" "${senhauser}" "${diasuser}" "${limiteuser}" && msg -ama "$(fun_trans "Usuario Modificado Com Sucesso")" || msg -verm "$(fun_trans "Erro, Usuario nao Modificado")"
msg -bar
}
detail_user () {
red=$(tput setaf 1)
gren=$(tput setaf 2)
yellow=$(tput setaf 3)
if [[ ! -e "${USRdatabase}" ]]; then
msg -verm "$(fun_trans "Nao Foi Identificado uma Database Com Usuarios")"
msg -verm "$(fun_trans "Os Usuarios a Seguir Nao Contem Nenhuma Informacao")"
msg -bar2
fi
txtvar=$(printf '%-16s' "USER")
txtvar+=$(printf '%-16s' "PASS")
txtvar+=$(printf '%-16s' "DATE")
txtvar+=$(printf '%-6s' "LIMIT")
echo -e "\033[1;33m${txtvar}"
msg -bar2
VPSsec=$(date +%s)
while read user; do
unset txtvar
data_user=$(chage -l "$user" |grep -i co |awk -F ":" '{print $2}')
txtvar=$(printf '%-21s' "${yellow}$user")
if [[ -e "${USRdatabase}" ]]; then
  if [[ $(cat ${USRdatabase}|grep -w "${user}") ]]; then
    txtvar+="$(printf '%-21s' "${yellow}$(cat ${USRdatabase}|grep -w "${user}"|cut -d'|' -f2)")"
    DateExp="$(cat ${USRdatabase}|grep -w "${user}"|cut -d'|' -f3)"
    DataSec=$(date +%s --date="$DateExp")
    if [[ "$VPSsec" -gt "$DataSec" ]]; then    
    EXPTIME="${red}[Exp]"
    else
    EXPTIME="${gren}[$(($(($DataSec - $VPSsec)) / 86400))]"
    fi
    txtvar+="$(printf '%-26s' "${yellow}${DateExp}${EXPTIME}")"
    txtvar+="$(printf '%-11s' "${yellow}$(cat ${USRdatabase}|grep -w "${user}"|cut -d'|' -f4)")"
    else
    txtvar+="$(printf '%-21s' "${red}???")"
    txtvar+="$(printf '%-21s' "${red}???")"
    txtvar+="$(printf '%-11s' "${red}???")"
  fi
fi
echo -e "$txtvar"
done <<< "$(mostrar_usuarios)"
msg -bar2
}
monit_user () {
yellow=$(tput setaf 3)
gren=$(tput setaf 2)
msg -verm "$(fun_trans "Monitor de Conexoes de Usuarios")"
msg -bar
txtvar=$(printf '%-13s' "USER")
txtvar+=$(printf '%-19s' "CONNECTION")
txtvar+=$(printf '%-16s' "TIME/ON")
echo -e "\033[1;33m${txtvar}"
msg -bar
while read user; do
 _=$(
PID="0+"
[[ $(dpkg --get-selections|grep -w "openssh"|head -1) ]] && PID+="$(ps aux|grep -v grep|grep sshd|grep -w "$user"|grep -v root|wc -l)+"
[[ $(dpkg --get-selections|grep -w "dropbear"|head -1) ]] && PID+="$(dropbear_pids|grep -w "${user}"|wc -l)+"
[[ $(dpkg --get-selections|grep -w "openvpn"|head -1) ]] && [[ -e /etc/openvpn/openvpn-status.log ]] && [[ $(openvpn_pids|grep -w "$user"|cut -d'|' -f2) ]] && PID+="$(openvpn_pids|grep -w "$user"|cut -d'|' -f2)+"
PID+="0"
TIMEON="${TIMEUS[$user]}"
[[ -z $TIMEON ]] && TIMEON=0
MIN=$(($TIMEON/60))
SEC=$(($TIMEON-$MIN*60))
HOR=$(($MIN/60))
MIN=$(($MIN-$HOR*60))
HOUR="${HOR}h:${MIN}m:${SEC}s"
[[ -z $(cat ${USRdatabase}|grep -w "${user}") ]] && MAXUSER="?" || MAXUSER="$(cat ${USRdatabase}|grep -w "${user}"|cut -d'|' -f4)"
[[ $(echo $PID|bc) -gt 0 ]] && user="$user [\033[1;32mON\033[0m${yellow}]" || user="$user [\033[1;31mOFF\033[0m${yellow}]"
TOTALPID="$(echo $PID|bc)/$MAXUSER"
 while [[ ${#user} -lt 45 ]]; do
 user=$user" "
 done
 while [[ ${#TOTALPID} -lt 13 ]]; do
 TOTALPID=$TOTALPID" "
 done
 while [[ ${#HOUR} -lt 8 ]]; do
 HOUR=$HOUR" "
 done
echo -e "${yellow}$user $TOTALPID $HOUR" >&2
) &
pid=$!
sleep 0.5s
done <<< "$(mostrar_usuarios)"
while [[ -d /proc/$pid ]]; do
sleep 1s
done
msg -bar
}
rm_vencidos () {
red=$(tput setaf 1)
gren=$(tput setaf 2)
yellow=$(tput setaf 3)
txtvar=$(printf '%-25s' "USER")
txtvar+=$(printf '%-20s' "VALID")
echo -e "\033[1;33m${txtvar}"
msg -bar
expired="${red}$(fun_trans "Expirado")"
valid="${gren}$(fun_trans "Usuario Valido")"
never="${yellow}$(fun_trans "Usuario Ilimitado")"
removido="${red}$(fun_trans "Removido")"
DataVPS=$(date +%s)
while read user; do
DataUser=$(chage -l "${user}" |grep -i co|awk -F ":" '{print $2}')
usr=$user
 while [[ ${#usr} -lt 20 ]]; do
 usr=$usr" "
 done
[[ "$DataUser" = " never" ]] && {
   echo -e "${yellow}$usr $never"
   continue
   }
DataSEC=$(date +%s --date="$DataUser")
if [[ "$DataSEC" -lt "$DataVPS" ]]; then
echo -ne "${yellow}$usr $expired"
rm_user "$user" && echo -e "($removido)"
else
echo -e "${yellow}$usr $valid"
fi
done <<< "$(mostrar_usuarios)"
msg -bar
}
verif_fun () {
# DECLARANDO VARIAVEIS PRIMARIAS
    local conexao
    local limite
    local TIMEUS
    declare -A conexao
    declare -A limite
    declare -A TIMEUS
    local LIMITERLOG="${SCPusr}/Limiter.log"
    [[ $(dpkg --get-selections|grep -w "openssh"|head -1) ]] && local SSH=ON || local SSH=OFF
    [[ $(dpkg --get-selections|grep -w "dropbear"|head -1) ]] && local DROP=ON || local DROP=OFF
    [[ $(dpkg --get-selections|grep -w "openvpn"|head -1) ]] && [[ -e /etc/openvpn/openvpn-status.log ]] && local OPEN=ON || local OPEN=OFF
    while true; do
    unset EXPIRED
    unset ONLINES
    [[ -e ${MyTIME} ]] && source ${MyTIME}
    local TimeNOW=$(date +%s)
    # INICIA VERIFICAO
    while read user; do
           echo -ne "\033[1;33mUSUARIO: \033[1;32m$user "
           if [[ ! $(echo $(mostrar_usuarios)|grep -w "$user") ]]; then
              echo -e "\033[1;31mNAO EXISTE"
              continue
           fi
           local DataUser=$(chage -l "${user}" |grep -i co|awk -F ":" '{print $2}')
           if [[ ! -z "$(echo $DataUser|grep never)" ]]; then
               echo -e "\033[1;31mILIMITADO" 
               continue
           fi
           local DataSEC=$(date +%s --date="$DataUser")
           if [[ "$DataSEC" -lt "$TimeNOW" ]]; then
              EXPIRED="1+"          
              block_userfun $user -loked && echo "USER: $user (LOKED - EXPIRED) $(date +%r)" >> $LIMITERLOG
              echo -e "\033[1;31m EXPIRADO"
              continue
           fi
           local PID="0+"
           [[ $SSH = ON  ]] && PID+="$(ps aux|grep -v grep|grep sshd|grep -w "$user"|grep -v root|wc -l 2>/dev/null)+"
           [[ $DROP = ON  ]] && PID+="$(dropbear_pids|grep -w "$user"|wc -l 2>/dev/null)+"
           [[ $OPEN = ON  ]] && [[ $(openvpn_pids|grep -w "$user"|cut -d'|' -f2) ]] && PID+="$(openvpn_pids|grep -w "$user"|cut -d'|' -f2)+"
           local ONLINES+="$(echo ${PID}0|bc)+"
           local conexao[$user]="$(echo ${PID}0|bc)"
            if [[ ${conexao[$user]} -gt '0' ]]; then #CONTADOR DE TEMPO ONLINE
              [[ -z "${TIMEUS[$user]}" ]] && local TIMEUS[$user]=0
              [[ "${TIMEUS[$user]}" != +([0-9]) ]] && local TIMEUS[$user]=0
              local TIMEUS[$user]="$((2+${TIMEUS[$user]}))"
              local VARS="$(cat ${MyTIME}|grep -w -v "$user")"
              echo "TIMEUS[$user]='${TIMEUS[$user]}'" > ${MyTIME}
              for variavel in $(echo ${VARS}); do echo "${variavel}" >> ${MyTIME}; done
            fi           
           local limite[$user]="$(cat ${USRdatabase}|grep -w "${user}"|cut -d'|' -f4)"
           [[ -z "${limite[$user]}" ]] && continue
           [[ "${limite[$user]}" != +([0-9]) ]] && continue
           if [[ "${conexao[$user]}" -gt "${limite[$user]}" ]]; then
           local lock=$(block_userfun $user -loked)
           [[ ! -z $lock ]] && echo "$lock" >> $LIMITERLOG
           echo -e "\033[1;31m ULTRAPASSOU LIMITE"
           continue
           fi
           echo -e "\033[1;33m OK! \033[1;31m${conexao[$user]} CONEXOES"
    done <<< "$(mostrar_usuarios)"
    echo "${ONLINES}0"|bc > ${SCPdir}/USRonlines
    echo "${EXPIRED}0"|bc > ${SCPdir}/USRexpired
    sleep 2s # TEMPO DE ESPERA DO LOOP
    clear
    done
}
backup_fun () {
msg -ama "$(fun_trans "FERRAMENTA DE BACKUP DE USUARIOS")"
msg -bar
menu_func "CRIAR BACKUP" "RESTAURAR BACKUP"
msg -bar
unset selection
while [[ ${selection} != @([1-2]) ]]; do
echo -ne "\033[1;37m$(fun_trans "Selecione A Opcao"): " && read selection
tput cuu1 && tput dl1
done
case ${selection} in
1)
cp ${USRdatabase} $HOME/Backup-adm
msg -azu "$(fun_trans "Procedimento Feito")"
echo -e "\033[1;31mBACKUP > [\033[1;32m$HOME/Backup-adm\033[1;31m]"
;;
2)
while [[ ! -e ${dirbackup} ]]; do
echo -ne "\033[1;37m$(fun_trans "Digite o Local Do Backup"): " && read dirbackup
tput cuu1 && tput dl1
done
VPSsec=$(date +%s)
while read line; do
nome=$(echo ${line}|cut -d'|' -f1)
[[ $(echo $(mostrar_usuarios)|grep -w "$nome") ]] && {
  msg -verm "$nome [ERROR]"
  continue
  }
senha=$(echo ${line}|cut -d'|' -f2)
DateExp=$(echo ${line}|cut -d'|' -f3)
DataSec=$(date +%s --date="$DateExp")
[[ "$VPSsec" -lt "$DataSec" ]] && dias="$(($(($DataSec - $VPSsec)) / 86400))" || dias="30"
limite=$(echo ${line}|cut -d'|' -f4)
add_user "$nome" "$senha" "$dias" "$limite" && msg -verd "$nome [OK]" || msg -verm "$nome [ERROR]"
done < ${dirbackup}
;;
esac
msg -bar
}
verif_funx () {
PIDVRF="$(ps aux|grep "${SCPusr}/usercodes verificar"|grep -v grep|awk '{print $2}')"
if [[ -z $PIDVRF ]]; then
cd ${SCPusr}
screen -dmS very ${SCPusr}/usercodes verificar
else
for pid in $(echo $PIDVRF); do
kill -9 $pid &>/dev/null
done
[[ -e ${SCPdir}/USRonlines ]] && rm ${SCPdir}/USRonlines
[[ -e ${SCPdir}/USRexpired ]] && rm ${SCPdir}/USRexpired
fi
}
baner_fun () {
local2="/etc/dropbear/banner"
chk=$(cat /etc/ssh/sshd_config | grep -v "Banner")
while read line; do
echo "$line" >> /tmp/ssh-conf
done <<< "$chk"
mv -f /tmp/ssh-conf /etc/ssh/sshd_config
echo "Banner /etc/bannerssh" >> /etc/ssh/sshd_config
local="/etc/bannerssh"
msg -verd "$(fun_trans "Bem vindo esse e o instalador do banner")"
msg -bar
msg -ne "$(fun_trans "digite a mensagem principal do banner"): " && read ban_ner
echo -e " \033[1;32m[1] >\033[1;32m $(fun_trans "Verde")"
echo -e " \033[1;32m[2] >\033[1;31m $(fun_trans "Vermelho")"
echo -e " \033[1;32m[3] >\033[1;34m $(fun_trans "Azul")"
echo -e " \033[1;32m[4] >\033[1;33m $(fun_trans "Amarelo")"
echo -e " \033[1;32m[5] >\033[1;35m $(fun_trans "Roxo")"
msg -ne "$(fun_trans "Perfeito Agora a Cor"): " && read ban_ner_cor
echo '<h1><font>=============================</font></h1>' > $local
if [[ "$ban_ner_cor" = "1" ]]; then
echo '<h1><font color="green">' >> $local
elif [[ "$ban_ner_cor" = "2" ]]; then
echo '<h1><font color="red">' >> $local
elif [[ "$ban_ner_cor" = "3" ]]; then
echo '<h1><font color="blue">' >> $local
elif [[ "$ban_ner_cor" = "4" ]]; then
echo '<h1><font color="yellow">' >> $local
elif [[ "$ban_ner_cor" = "5" ]]; then
echo '<h1><font color="purple">' >> $local
else
echo '<h1><font color="blue">' >> $local
fi
echo "$ban_ner" >> $local
echo '</font></h1>' >> $local
echo '<h1><font>=============================</font></h1>' >> $local
txt_font () {
msg -ne "$(fun_trans "digite a mensagem secundaria"): " && read ban_ner2
echo -e " \033[1;32m[1] >\033[1;32m $(fun_trans "Verde")"
echo -e " \033[1;32m[2] >\033[1;31m $(fun_trans "Vermelho")"
echo -e " \033[1;32m[3] >\033[1;34m $(fun_trans "Azul")"
echo -e " \033[1;32m[4] >\033[1;33m $(fun_trans "Amarelo")"
echo -e " \033[1;32m[5] >\033[1;35m $(fun_trans "Roxo")"
msg -ne "$(fun_trans "Perfeito Agora a Cor"): " && read ban_ner2_cor
if [ "$ban_ner2_cor" = "1" ]; then
echo '<h6><font color="green">' >> $local
elif [ "$ban_ner2_cor" = "2" ]; then
echo '<h6><font color="red">' >> $local
elif [ "$ban_ner2_cor" = "3" ]; then
echo '<h6><font color="blue">' >> $local
elif [ "$ban_ner2_cor" = "4" ]; then
echo '<h6><font color="yellow">' >> $local
elif [ "$ban_ner2_cor" = "5" ]; then
echo '<h6><font color="purple">' >> $local
else
echo '<h6><font color="red">' >> $local
fi
echo "$ban_ner2" >> $local
echo "</h6></font>" >> $local
}
while true; do
msg -ne "$(fun_trans "Adicionar Mensagem Secundaria")? [S/N]: " && read sin_nao
if [[ "$sin_nao" = @(s|S|y|Y) ]]; then
txt_font
elif [[ "$sin_nao" = @(n|N) ]]; then
break
fi
done
#echo '</h8><font color="purple">new</font></h8>' >> $local
#echo '<h1><font>=============================</font></h1>' >> $local
if [[ -e "$local2" ]]; then
rm $local2  > /dev/null 2>&1
cp $local $local2 > /dev/null 2>&1
fi
msg -bar && msg -verd "$(fun_trans "Banner Adicionado Com Sucesso")" && msg -bar
service ssh restart > /dev/null 2>&1 &
service sshd restart > /dev/null 2>&1 & 
service dropbear restart > /dev/null 2>&1 &
}
# Execuo
if [[ "$1" = "verificar" ]]; then
verif_fun
exit
fi
[[ -z ${VERY} ]] && verificar="\033[1;31mOff" || verificar="\033[1;32mOn"
msg -ama "$(fun_trans "MENU GERENCIAMENTO DE USUARIOS")"
msg -bar
menu_func "CRIAR NOVO USUARIO" "REMOVER USUARIO" "BLOQUEAR OU DESBLOQUEAR USUARIO" "EDITAR USUARIO" "RENOVAR USUARIO" "DETALHES DE TODOS USUARIOS" "MONITORAR USUARIOS CONECTADOS" "ELIMINAR USUARIOS VENCIDOS" "BACKUP USUARIOS" "BANNER SSH" "-fi ${verificar} VERIFICACOES"
[[ -e "${SCPusr}/Limiter.log" ]] && echo -ne "$(msg -verd "[12]") $(msg -verm2 ">") " && msg -azu "$(fun_trans "VER LOG DO LIMITER")"
echo -ne "$(msg -verd "[0]") $(msg -verm2 ">") " && msg -bra "$(fun_trans "VOLTAR")"
msg -bar
selection=$(selection_fun 12)
case ${selection} in
1)new_user;;
2)remove_user;;
3)block_user;;
4)edit_user;;
5)renew_user;;
6)detail_user;;
7)monit_user;;
8)rm_vencidos;;
9)backup_fun;;
10)baner_fun;;
11)verif_funx;;
12)
[[ -e "${SCPusr}/Limiter.log" ]] && {
 cat ${SCPusr}/Limiter.log
 msg -bar
 }
;;
esac