source: trunk/cgi-bin/access-pwd-update.pl@ 405

Last change on this file since 405 was 399, checked in by Kris Deugau, 15 years ago

/trunk

Merge updates/bugfixes to access-pwd-update.pl from r386. See #13.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 1.7 KB
Line 
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-05-11 18:59:27 +0000 (Tue, 11 May 2010) $
7# SVN revision $Rev: 399 $
8# Last update by $Author: kdeugau $
9###
10# Copyright (C) 2007,2008 - Kris Deugau
11
12use strict;
13use warnings;
14use DBI;
15use lib '/var/www/ipdb.example.com/ip/cgi-bin/';
16use MyIPDB;
17
18my $ip_dbh;
19my $errstr;
20($ip_dbh,$errstr) = connectDB_My;
21if (!$ip_dbh) {
22 die "Database error: $errstr\n";
23}
24initIPDBGlobals($ip_dbh);
25
26my %userhash;
27my $passfile = "/var/www/ipdb.example.com/ip/.htpasswd";
28
29die ".htpasswd error: file is empty!\n"
30 if -z $passfile;
31
32die ".htpasswd error: file seems too small: ".(-s $passfile)."\n"
33 if (-s $passfile <3000);
34
35open HTPASS, "<$passfile";
36
37my $sth = $ip_dbh->prepare("select count(*) from users where username=?");
38my $sth2;
39while (<HTPASS>) {
40 chomp;
41 my ($user,$pass) = split /:/;
42 $sth->execute($user);
43 my @data = $sth->fetchrow_array();
44 my $sql;
45 if ($data[0] == 0) {
46 $sql = "insert into users (username,password) values ('$user','$pass')";
47 print "new user: $user\n";
48 } else {
49 $sql = "update users set password='$pass' where username='$user'";
50 }
51 $sth2 = $ip_dbh->prepare($sql);
52 $sth2->execute or print "error executing $sql: ".$DBI::errstr."\n";
53 $userhash{$user} = $pass;
54}
55
56# and now to delete users that have been removed
57$sth = $ip_dbh->prepare("select username,acl from users order by username");
58$sth2 = $ip_dbh->prepare("delete from users where username=?");
59$sth->execute;
60while (my @data = $sth->fetchrow_array()) {
61 if (!$userhash{$data[0]}) {
62 print "deleting $data[0] (acl $data[1])\n";
63 $sth2->execute($data[0])
64 or print "error deleting $data[0]: ".$DBI::errstr."\n";
65 }
66}
Note: See TracBrowser for help on using the repository browser.