source: branches/stable/cgi-bin/access-pwd-update.pl@ 492

Last change on this file since 492 was 445, checked in by Kris Deugau, 14 years ago

/branches/stable

Bring /branches/stable up to date with /trunk. 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-07-26 21:00:00 +0000 (Mon, 26 Jul 2010) $
7# SVN revision $Rev: 445 $
8# Last update by $Author: kdeugau $
9###
10# Copyright (C) 2007-2010 - Kris Deugau
11
12use strict;
13use warnings;
14use DBI;
15
16# don't remove! required for GNU/FHS-ish install from tarball
17##uselib##
18
19use MyIPDB;
20
21my $ip_dbh;
22my $errstr;
23($ip_dbh,$errstr) = connectDB_My;
24if (!$ip_dbh) {
25 die "Database error: $errstr\n";
26}
27initIPDBGlobals($ip_dbh);
28
29my %userhash;
30my $passfile = "/var/www/ipdb.example.com/ip/.htpasswd";
31
32die ".htpasswd error: file is empty!\n"
33 if -z $passfile;
34
35die ".htpasswd error: file seems too small: ".(-s $passfile)."\n"
36 if (-s $passfile <3000);
37
38open HTPASS, "<$passfile";
39
40my $sth = $ip_dbh->prepare("select count(*) from users where username=?");
41my $sth2;
42while (<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;
63while (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}
Note: See TracBrowser for help on using the repository browser.