#!/usr/bin/perl # # squid authenticator module # using MySQL database # # Author: Leo Fellermayr (leosmail@mac.com) # # $Id: squid_auth_mysql.pl,v 1.0 2002/06/06 13:08:25 leo Exp $ # use POSIX qw(strftime); use DBI(); use strict; my $msg_ok = "OK\n"; my $msg_err = "ERR\n"; my $db_name = "misc"; my $db_table = "proxyauth"; my $db_host = "localhost"; my $db_user = "root"; my $db_pass = "*SECRET*"; my $dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host", $db_user, $db_pass, {'RaiseError' => 1}); sub query_db ($$) { my $this_user = shift; my $this_pass = shift; return 0 if ((!$this_user) | (!$this_pass)); my $sth = $dbh->prepare ("SELECT username FROM $db_table WHERE username LIKE '" . $this_user . "' AND password LIKE '" . $this_pass . "'"); $sth->execute (); my $retval = $sth->rows; $sth->finish (); return $retval; } # ----- main program # flush stdout (important, without this squid ends in an infinite stuck loop... # took me 1,5 hours to find this out ;-) $| = 1; # wait for input from stdin while () { chomp; my ($username, $password) = split / /; if (query_db (lc $username, $password)) { print $msg_ok; } else { print $msg_err; } } $dbh->disconnect (); exit (0);