--------------------- PatchSet 11781 Date: 2007/11/26 11:06:52 Author: adrian Branch: SQUID_2_6 Tag: (none) Log: Author: hno Patchset 11747 (HEAD): Make squid_db_auth reopen the database connection on each query by default This change adds support for non-persistent database connections, and also makes that the default. To use a persistent database connection use the --persist option. Members: helpers/basic_auth/DB/squid_db_auth.in:1.1.2.3->1.1.2.4 Index: squid/helpers/basic_auth/DB/squid_db_auth.in =================================================================== RCS file: /cvsroot/squid/squid/helpers/basic_auth/DB/squid_db_auth.in,v retrieving revision 1.1.2.3 retrieving revision 1.1.2.4 diff -u -r1.1.2.3 -r1.1.2.4 --- squid/helpers/basic_auth/DB/squid_db_auth.in 31 Aug 2007 13:47:03 -0000 1.1.2.3 +++ squid/helpers/basic_auth/DB/squid_db_auth.in 26 Nov 2007 11:06:52 -0000 1.1.2.4 @@ -21,6 +21,7 @@ my $db_passwdcol = "password"; my $db_cond = "enabled = 1"; my $plaintext = 0; +my $persist = 0; =pod @@ -66,6 +67,10 @@ Database contains plain-text passwords +=item B<--persist> + +Keep a persistent database connection open between queries. + =back =cut @@ -79,13 +84,30 @@ 'passwdcol=s' => \$db_passwdcol, 'cond=s' => \$db_cond, 'plaintext' => \$plaintext, + 'persist' => \$persist, ); -my $dbh = DBI->connect($dsn, $db_user, $db_passwd) || die ("Could not connect to $dsn\n"); +my ($_dbh, $_sth); -my ($sth) = $dbh->prepare("SELECT $db_passwdcol FROM $db_table WHERE $db_usercol = ?" . ($db_cond ne "" ? " AND $db_cond" : "")) || die; +sub close_db() +{ + return if !defined($_dbh); + $_dbh->disconnect(); + undef $_dbh; + undef $_sth; +} -my $status; +sub open_db() +{ + return $_sth if defined $_sth; + $_dbh = DBI->connect($dsn, $db_user, $db_passwd); + if (!defined $_dbh) { + warn ("Could not connect to $dsn\n"); + return undef; + } + $_sth = $_dbh->prepare("SELECT $db_passwdcol FROM $db_table WHERE $db_usercol = ?" . ($db_cond ne "" ? " AND $db_cond" : "")) || die; + return $_sth; +} sub check_password($$) { @@ -97,20 +119,34 @@ return 0; } + +sub query_db($) { + my ($user) = @_; + my ($sth) = open_db() || return undef; + if (!$sth->execute($user)) { + close_db(); + open_db() || return undef; + $sth->execute($user) || return undef;; + } + return $sth; +} +my $status; + while (<>) { my ($user, $password) = split; $status = "ERR"; $user =~ s/%(..)/pack("H*", $1)/ge; $password =~ s/%(..)/pack("H*", $1)/ge; - $status = "ERR internal error"; - $sth->execute($user) || next; + $status = "ERR database error"; + my $sth = query_db($user) || next; $status = "ERR unknown login"; - my ($row) = $sth->fetchrow_arrayref() || next; + my $row = $sth->fetchrow_arrayref() || next; $status = "ERR login failure"; next if (!check_password($password, @$row[0])); $status = "OK"; } continue { + close_db() if (!$persist); print $status . "\n"; }