[373] | 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-07-26 21:00:00 +0000 (Mon, 26 Jul 2010) $
|
---|
| 7 | # SVN revision $Rev: 445 $
|
---|
| 8 | # Last update by $Author: kdeugau $
|
---|
| 9 | ###
|
---|
[445] | 10 | # Copyright (C) 2007-2010 - Kris Deugau
|
---|
[373] | 11 |
|
---|
| 12 | use strict;
|
---|
| 13 | use warnings;
|
---|
| 14 | use DBI;
|
---|
[445] | 15 |
|
---|
| 16 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
| 17 | ##uselib##
|
---|
| 18 |
|
---|
[373] | 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;
|
---|
[386] | 30 | my $passfile = "/var/www/ipdb.example.com/ip/.htpasswd";
|
---|
[373] | 31 |
|
---|
[386] | 32 | die ".htpasswd error: file is empty!\n"
|
---|
| 33 | if -z $passfile;
|
---|
[373] | 34 |
|
---|
[386] | 35 | die ".htpasswd error: file seems too small: ".(-s $passfile)."\n"
|
---|
| 36 | if (-s $passfile <3000);
|
---|
| 37 |
|
---|
| 38 | open HTPASS, "<$passfile";
|
---|
| 39 |
|
---|
[373] | 40 | my $sth = $ip_dbh->prepare("select count(*) from users where username=?");
|
---|
[386] | 41 | my $sth2;
|
---|
[373] | 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')";
|
---|
[386] | 50 | print "new user: $user\n";
|
---|
[373] | 51 | } else {
|
---|
| 52 | $sql = "update users set password='$pass' where username='$user'";
|
---|
| 53 | }
|
---|
[386] | 54 | $sth2 = $ip_dbh->prepare($sql);
|
---|
[373] | 55 | $sth2->execute or print "error executing $sql: ".$DBI::errstr."\n";
|
---|
| 56 | $userhash{$user} = $pass;
|
---|
| 57 | }
|
---|
[386] | 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 | }
|
---|
[373] | 69 | }
|
---|