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: 2007-12-11 20:27:18 +0000 (Tue, 11 Dec 2007) $
|
---|
7 | # SVN revision $Rev: 374 $
|
---|
8 | # Last update by $Author: kdeugau $
|
---|
9 | ###
|
---|
10 | # Copyright (C) 2007 - Kris Deugau
|
---|
11 |
|
---|
12 | use strict;
|
---|
13 | use warnings;
|
---|
14 | use DBI;
|
---|
15 | use MyIPDB;
|
---|
16 |
|
---|
17 | my $ip_dbh;
|
---|
18 | my $errstr;
|
---|
19 | ($ip_dbh,$errstr) = connectDB_My;
|
---|
20 | if (!$ip_dbh) {
|
---|
21 | die "Database error: $errstr\n";
|
---|
22 | }
|
---|
23 | initIPDBGlobals($ip_dbh);
|
---|
24 |
|
---|
25 | my %userhash;
|
---|
26 |
|
---|
27 | open HTPASS, "</var/www/ipdb.example.com/ip/.htpasswd";
|
---|
28 |
|
---|
29 | my $sth = $ip_dbh->prepare("select count(*) from users where username=?");
|
---|
30 |
|
---|
31 | while (<HTPASS>) {
|
---|
32 | chomp;
|
---|
33 | my ($user,$pass) = split /:/;
|
---|
34 | $sth->execute($user);
|
---|
35 | my @data = $sth->fetchrow_array();
|
---|
36 | my $sql;
|
---|
37 | if ($data[0] == 0) {
|
---|
38 | $sql = "insert into users (username,password) values ('$user','$pass')";
|
---|
39 | } else {
|
---|
40 | $sql = "update users set password='$pass' where username='$user'";
|
---|
41 | }
|
---|
42 | my $sth2 = $ip_dbh->prepare($sql);
|
---|
43 | $sth2->execute or print "error executing $sql: ".$DBI::errstr."\n";
|
---|
44 | $userhash{$user} = $pass;
|
---|
45 | }
|
---|
46 | my @userlist = @{ $ip_dbh->selectall_arrayref("select username from users") };
|
---|
47 | $sth = $ip_dbh->prepare("delete from users where username=?");
|
---|
48 | foreach my $user (@userlist) {
|
---|
49 | $sth->execute if !$userhash{$user}
|
---|
50 | or print "error deleting $user: ".$DBI::errstr."\n";
|
---|
51 | }
|
---|