[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-06-30 21:48:03 +0000 (Wed, 30 Jun 2010) $
|
---|
| 7 | # SVN revision $Rev: 417 $
|
---|
| 8 | # Last update by $Author: kdeugau $
|
---|
| 9 | ###
|
---|
[417] | 10 | # Copyright (C) 2007-2010 - Kris Deugau
|
---|
[373] | 11 |
|
---|
| 12 | use strict;
|
---|
| 13 | use warnings;
|
---|
| 14 | use DBI;
|
---|
[417] | 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;
|
---|
[399] | 30 | my $passfile = "/var/www/ipdb.example.com/ip/.htpasswd";
|
---|
[373] | 31 |
|
---|
[399] | 32 | die ".htpasswd error: file is empty!\n"
|
---|
| 33 | if -z $passfile;
|
---|
[373] | 34 |
|
---|
[399] | 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=?");
|
---|
[399] | 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')";
|
---|
[399] | 50 | print "new user: $user\n";
|
---|
[373] | 51 | } else {
|
---|
| 52 | $sql = "update users set password='$pass' where username='$user'";
|
---|
| 53 | }
|
---|
[399] | 54 | $sth2 = $ip_dbh->prepare($sql);
|
---|
[373] | 55 | $sth2->execute or print "error executing $sql: ".$DBI::errstr."\n";
|
---|
| 56 | $userhash{$user} = $pass;
|
---|
| 57 | }
|
---|
[399] | 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 | }
|
---|