| 1 | #!/usr/bin/perl
|
|---|
| 2 | # Update IPDB users table with user/password data from 5-minute
|
|---|
| 3 | # cron'ed push from billing
|
|---|
| 4 | ###
|
|---|
| 5 | # SVN revision info
|
|---|
| 6 | # $Date: 2010-05-11 18:59:27 +0000 (Tue, 11 May 2010) $
|
|---|
| 7 | # SVN revision $Rev: 399 $
|
|---|
| 8 | # Last update by $Author: kdeugau $
|
|---|
| 9 | ###
|
|---|
| 10 | # Copyright (C) 2007,2008 - Kris Deugau
|
|---|
| 11 |
|
|---|
| 12 | use strict;
|
|---|
| 13 | use warnings;
|
|---|
| 14 | use DBI;
|
|---|
| 15 | use lib '/var/www/ipdb.example.com/ip/cgi-bin/';
|
|---|
| 16 | use MyIPDB;
|
|---|
| 17 |
|
|---|
| 18 | my $ip_dbh;
|
|---|
| 19 | my $errstr;
|
|---|
| 20 | ($ip_dbh,$errstr) = connectDB_My;
|
|---|
| 21 | if (!$ip_dbh) {
|
|---|
| 22 | die "Database error: $errstr\n";
|
|---|
| 23 | }
|
|---|
| 24 | initIPDBGlobals($ip_dbh);
|
|---|
| 25 |
|
|---|
| 26 | my %userhash;
|
|---|
| 27 | my $passfile = "/var/www/ipdb.example.com/ip/.htpasswd";
|
|---|
| 28 |
|
|---|
| 29 | die ".htpasswd error: file is empty!\n"
|
|---|
| 30 | if -z $passfile;
|
|---|
| 31 |
|
|---|
| 32 | die ".htpasswd error: file seems too small: ".(-s $passfile)."\n"
|
|---|
| 33 | if (-s $passfile <3000);
|
|---|
| 34 |
|
|---|
| 35 | open HTPASS, "<$passfile";
|
|---|
| 36 |
|
|---|
| 37 | my $sth = $ip_dbh->prepare("select count(*) from users where username=?");
|
|---|
| 38 | my $sth2;
|
|---|
| 39 | while (<HTPASS>) {
|
|---|
| 40 | chomp;
|
|---|
| 41 | my ($user,$pass) = split /:/;
|
|---|
| 42 | $sth->execute($user);
|
|---|
| 43 | my @data = $sth->fetchrow_array();
|
|---|
| 44 | my $sql;
|
|---|
| 45 | if ($data[0] == 0) {
|
|---|
| 46 | $sql = "insert into users (username,password) values ('$user','$pass')";
|
|---|
| 47 | print "new user: $user\n";
|
|---|
| 48 | } else {
|
|---|
| 49 | $sql = "update users set password='$pass' where username='$user'";
|
|---|
| 50 | }
|
|---|
| 51 | $sth2 = $ip_dbh->prepare($sql);
|
|---|
| 52 | $sth2->execute or print "error executing $sql: ".$DBI::errstr."\n";
|
|---|
| 53 | $userhash{$user} = $pass;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | # and now to delete users that have been removed
|
|---|
| 57 | $sth = $ip_dbh->prepare("select username,acl from users order by username");
|
|---|
| 58 | $sth2 = $ip_dbh->prepare("delete from users where username=?");
|
|---|
| 59 | $sth->execute;
|
|---|
| 60 | while (my @data = $sth->fetchrow_array()) {
|
|---|
| 61 | if (!$userhash{$data[0]}) {
|
|---|
| 62 | print "deleting $data[0] (acl $data[1])\n";
|
|---|
| 63 | $sth2->execute($data[0])
|
|---|
| 64 | or print "error deleting $data[0]: ".$DBI::errstr."\n";
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|