10/05/2007

Squid Autenticando no MySQL

Requisitos:
- Base MySQL com "alguma" tabela/view que contenha usuário e senha, para fazer a autenticacao;
- Squid instalado;

Script de Autenticação em Shell
- Criar o script, por exemplo:
touch > /etc/squid/auth_squid.sh
chmod 775 /etc/squid/auth_squid.sh

- Editar o script: vi /etc/squid/auth_squid.sh e adicionar o seguinte conteúdo no arquivo.

#!/bin/sh

MY="mysql -u root nomebasededados -e "
i=0
while [ $i -lt 4 ]
do
QRY="SELECT IF(COUNT(*) = 0,'ERR','OK') as NUM
FROM tabela_de_auth
WHERE LOWER(login) = LOWER('pLOGIN') AND
LOWER(senha) = LOWER('pSENHA')"

read vlr;

LOGIN=`echo $vlr | awk '{print $1}'`
SENHA=`echo $vlr | awk '{print $2}'`

if [ -z "$LOGIN" ]
then
echo "ERR"
i=$((i+1))
elif [ -z "$SENHA" ]
then
echo "ERR"
i=$((i+1))
else

QRY=`echo $QRY | sed "s/pLOGIN/$LOGIN/g"`
QRY=`echo $QRY | sed "s/pSENHA/$SENHA/g"`

$MY "$QRY" | while read res;
do

if test $res != "NUM"
then
echo $res

if test $res == "ERR"
then
i=$(expr $i + 1)
fi

fi

done

fi

done