Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use DBI;
- use Digest::MD5;
- my $data_source = 'DBI:Pg:dbname=main_db;host=data.inner.tumgasa.ru';
- my $username = 'ctl_user';
- my $password = '77FlatWonder';
- my $query = "SELECT user_pwd FROM squid_users WHERE user_name = ? AND state = 0";
- my $cache_ttl = 600;
- my $dbh = DBI->connect($data_source, $username, $password);
- my $method = $ARGV[0];
- my $basic = 1;
- unless($method eq 'basic' or $method eq 'digest') {
- print STDERR "Usage: $0 (basic|digest)\n";
- exit 1;
- }
- $basic = undef if $method eq 'digest';
- my $sth = $dbh->prepare($query);
- my %pwd_cache;
- $| = 1;
- while (<STDIN>) {
- chomp;
- my($username, $realm, $supplied_password);
- if($basic) {
- ($username, $supplied_password) = split;
- } else {
- ($username, $realm) = /^"(.*)":"(.*)"$/;
- }
- print STDERR "$username, $supplied_password, $realm\n";
- unless(length($username) or length($realm)) {
- print "ERR\n";
- next;
- }
- my $now = time;
- my $cached = $pwd_cache{$username};
- my $password;
- if($cached) {
- ($password, my $time) = @$cached;
- if($time < $now - $cache_ttl) {
- $password = undef;
- }
- }
- unless(defined $password) {
- $sth->execute($username);
- my @row = $sth->fetchrow;
- unless(@row) {
- print "ERR\n";
- next;
- }
- $password = $row[0];
- $pwd_cache{$username} = [$password, $now];
- $sth->finish;
- }
- if($basic) {
- if($password eq $supplied_password) {
- print "OK\n";
- } else {
- print "ERR\n";
- }
- } else {
- my $ctx = Digest::MD5->new;
- $ctx->add($username);
- $ctx->add(":");
- $ctx->add($realm);
- $ctx->add(":");
- $ctx->add($password);
- my $digest = $ctx->hexdigest;
- print "$digest\n";
- }
- }
- $dbh->disconnect;
Add Comment
Please, Sign In to add comment