Autenticação com usuário de email (pop3) no Squid

De Wiki Hackstore

Configuração squid auth

auth_param basic realm INTERNET
auth_param basic program /scripts/squid-auth.py
auth_param basic credentialsttl 5
auth_param basic children 5

Script python

#!/usr/bin/env python

from poplib import POP3
import sys


#POP server against which we authenticate
server="127.0.0.1"
domain="@hackstore.com.br"
#Port number for POP server. Usually 110
port=110


#Below here you shouldn't need to edit anything

while 1:

        #Read user and password from stdin, remove the newline, split at the space
        #and assign to the user and password variables
        line=sys.stdin.readline()[:-1]
        [user,password]=line.split(' ')

        #Connect to the POP server
        user=user+domain
#       user=user
        p=POP3(server,port)

        #Try to authenticate. If it doesn't work, it throws an exception

        try:
                p.user(user)
                p.pass_(password)
        except:

                #If it threw an exception, log in cache.log the ayth booboo
                sys.stderr.write("ERR authenticating %s\n"%user)
                #Then deny access
                sys.stdout.write("ERR\n")
                #IMPORTANT!!!!!!!!!!!! Flush stdout
                sys.stdout.flush()
                continue

        #If it didn't throw exceptions, that means it authenticated

        #Log success to cache.log
        sys.stderr.write("OK authenticated %s\n"%user)
        #Then allow access
        sys.stdout.write("OK\n")
        sys.stdout.flush()