Guest User

Untitled

a guest
Mar 6th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use DBI;
  5. use Digest::MD5;
  6.  
  7. my $data_source = 'DBI:Pg:dbname=main_db;host=data.inner.tumgasa.ru';
  8. my $username = 'ctl_user';
  9. my $password = '77FlatWonder';
  10. my $query = "SELECT user_pwd FROM squid_users WHERE user_name = ? AND state = 0";
  11.  
  12. my $cache_ttl = 600;
  13.  
  14. my $dbh = DBI->connect($data_source, $username, $password);
  15.  
  16. my $method = $ARGV[0];
  17.  
  18. my $basic = 1;
  19.  
  20. unless($method eq 'basic' or $method eq 'digest') {
  21. print STDERR "Usage: $0 (basic|digest)\n";
  22. exit 1;
  23. }
  24.  
  25. $basic = undef if $method eq 'digest';
  26.  
  27. my $sth = $dbh->prepare($query);
  28.  
  29. my %pwd_cache;
  30.  
  31. $| = 1;
  32.  
  33. while (<STDIN>) {
  34. chomp;
  35. my($username, $realm, $supplied_password);
  36.  
  37. if($basic) {
  38. ($username, $supplied_password) = split;
  39. } else {
  40. ($username, $realm) = /^"(.*)":"(.*)"$/;
  41. }
  42.  
  43. print STDERR "$username, $supplied_password, $realm\n";
  44.  
  45. unless(length($username) or length($realm)) {
  46. print "ERR\n";
  47. next;
  48. }
  49.  
  50. my $now = time;
  51. my $cached = $pwd_cache{$username};
  52.  
  53. my $password;
  54.  
  55. if($cached) {
  56. ($password, my $time) = @$cached;
  57. if($time < $now - $cache_ttl) {
  58. $password = undef;
  59. }
  60. }
  61.  
  62. unless(defined $password) {
  63. $sth->execute($username);
  64.  
  65. my @row = $sth->fetchrow;
  66.  
  67. unless(@row) {
  68. print "ERR\n";
  69. next;
  70. }
  71.  
  72. $password = $row[0];
  73. $pwd_cache{$username} = [$password, $now];
  74. $sth->finish;
  75. }
  76.  
  77. if($basic) {
  78. if($password eq $supplied_password) {
  79. print "OK\n";
  80. } else {
  81. print "ERR\n";
  82. }
  83. } else {
  84.  
  85. my $ctx = Digest::MD5->new;
  86. $ctx->add($username);
  87. $ctx->add(":");
  88. $ctx->add($realm);
  89. $ctx->add(":");
  90. $ctx->add($password);
  91. my $digest = $ctx->hexdigest;
  92.  
  93. print "$digest\n";
  94. }
  95. }
  96.  
  97. $dbh->disconnect;
Add Comment
Please, Sign In to add comment