[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: 2017-08-15 17:53:23 +0000 (Tue, 15 Aug 2017) $
|
---|
| 7 | # SVN revision $Rev: 906 $
|
---|
| 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 |
|
---|
[906] | 19 | # push "the directory the script is in" into @INC
|
---|
| 20 | use FindBin;
|
---|
| 21 | use lib "$FindBin::RealBin/";
|
---|
| 22 |
|
---|
[373] | 23 | use MyIPDB;
|
---|
| 24 |
|
---|
| 25 | my $ip_dbh;
|
---|
| 26 | my $errstr;
|
---|
| 27 | ($ip_dbh,$errstr) = connectDB_My;
|
---|
| 28 | if (!$ip_dbh) {
|
---|
| 29 | die "Database error: $errstr\n";
|
---|
| 30 | }
|
---|
| 31 | initIPDBGlobals($ip_dbh);
|
---|
| 32 |
|
---|
| 33 | my %userhash;
|
---|
[399] | 34 | my $passfile = "/var/www/ipdb.example.com/ip/.htpasswd";
|
---|
[373] | 35 |
|
---|
[399] | 36 | die ".htpasswd error: file is empty!\n"
|
---|
| 37 | if -z $passfile;
|
---|
[373] | 38 |
|
---|
[399] | 39 | die ".htpasswd error: file seems too small: ".(-s $passfile)."\n"
|
---|
| 40 | if (-s $passfile <3000);
|
---|
| 41 |
|
---|
| 42 | open HTPASS, "<$passfile";
|
---|
| 43 |
|
---|
[373] | 44 | my $sth = $ip_dbh->prepare("select count(*) from users where username=?");
|
---|
[399] | 45 | my $sth2;
|
---|
[373] | 46 | while (<HTPASS>) {
|
---|
| 47 | chomp;
|
---|
| 48 | my ($user,$pass) = split /:/;
|
---|
| 49 | $sth->execute($user);
|
---|
| 50 | my @data = $sth->fetchrow_array();
|
---|
| 51 | my $sql;
|
---|
| 52 | if ($data[0] == 0) {
|
---|
| 53 | $sql = "insert into users (username,password) values ('$user','$pass')";
|
---|
[399] | 54 | print "new user: $user\n";
|
---|
[373] | 55 | } else {
|
---|
| 56 | $sql = "update users set password='$pass' where username='$user'";
|
---|
| 57 | }
|
---|
[399] | 58 | $sth2 = $ip_dbh->prepare($sql);
|
---|
[373] | 59 | $sth2->execute or print "error executing $sql: ".$DBI::errstr."\n";
|
---|
| 60 | $userhash{$user} = $pass;
|
---|
| 61 | }
|
---|
[399] | 62 |
|
---|
| 63 | # and now to delete users that have been removed
|
---|
| 64 | $sth = $ip_dbh->prepare("select username,acl from users order by username");
|
---|
| 65 | $sth2 = $ip_dbh->prepare("delete from users where username=?");
|
---|
| 66 | $sth->execute;
|
---|
| 67 | while (my @data = $sth->fetchrow_array()) {
|
---|
| 68 | if (!$userhash{$data[0]}) {
|
---|
| 69 | print "deleting $data[0] (acl $data[1])\n";
|
---|
| 70 | $sth2->execute($data[0])
|
---|
| 71 | or print "error deleting $data[0]: ".$DBI::errstr."\n";
|
---|
| 72 | }
|
---|
[373] | 73 | }
|
---|