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-06-30 21:48:03 +0000 (Wed, 30 Jun 2010) $ |
---|
7 | # SVN revision $Rev: 417 $ |
---|
8 | # Last update by $Author: kdeugau $ |
---|
9 | ### |
---|
10 | # Copyright (C) 2007-2010 - Kris Deugau |
---|
11 | |
---|
12 | use strict; |
---|
13 | use warnings; |
---|
14 | use DBI; |
---|
15 | |
---|
16 | # don't remove! required for GNU/FHS-ish install from tarball |
---|
17 | ##uselib## |
---|
18 | |
---|
19 | use MyIPDB; |
---|
20 | |
---|
21 | my $ip_dbh; |
---|
22 | my $errstr; |
---|
23 | ($ip_dbh,$errstr) = connectDB_My; |
---|
24 | if (!$ip_dbh) { |
---|
25 | die "Database error: $errstr\n"; |
---|
26 | } |
---|
27 | initIPDBGlobals($ip_dbh); |
---|
28 | |
---|
29 | my %userhash; |
---|
30 | my $passfile = "/var/www/ipdb.example.com/ip/.htpasswd"; |
---|
31 | |
---|
32 | die ".htpasswd error: file is empty!\n" |
---|
33 | if -z $passfile; |
---|
34 | |
---|
35 | die ".htpasswd error: file seems too small: ".(-s $passfile)."\n" |
---|
36 | if (-s $passfile <3000); |
---|
37 | |
---|
38 | open HTPASS, "<$passfile"; |
---|
39 | |
---|
40 | my $sth = $ip_dbh->prepare("select count(*) from users where username=?"); |
---|
41 | my $sth2; |
---|
42 | while (<HTPASS>) { |
---|
43 | chomp; |
---|
44 | my ($user,$pass) = split /:/; |
---|
45 | $sth->execute($user); |
---|
46 | my @data = $sth->fetchrow_array(); |
---|
47 | my $sql; |
---|
48 | if ($data[0] == 0) { |
---|
49 | $sql = "insert into users (username,password) values ('$user','$pass')"; |
---|
50 | print "new user: $user\n"; |
---|
51 | } else { |
---|
52 | $sql = "update users set password='$pass' where username='$user'"; |
---|
53 | } |
---|
54 | $sth2 = $ip_dbh->prepare($sql); |
---|
55 | $sth2->execute or print "error executing $sql: ".$DBI::errstr."\n"; |
---|
56 | $userhash{$user} = $pass; |
---|
57 | } |
---|
58 | |
---|
59 | # and now to delete users that have been removed |
---|
60 | $sth = $ip_dbh->prepare("select username,acl from users order by username"); |
---|
61 | $sth2 = $ip_dbh->prepare("delete from users where username=?"); |
---|
62 | $sth->execute; |
---|
63 | while (my @data = $sth->fetchrow_array()) { |
---|
64 | if (!$userhash{$data[0]}) { |
---|
65 | print "deleting $data[0] (acl $data[1])\n"; |
---|
66 | $sth2->execute($data[0]) |
---|
67 | or print "error deleting $data[0]: ".$DBI::errstr."\n"; |
---|
68 | } |
---|
69 | } |
---|