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

Last change on this file since 906 was 906, checked in by Kris Deugau, 7 years ago

/trunk

Bulk addition of "add 'the directory the script is in' to @INC" for Perls
that have dropped '.' from @INC

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 1.8 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: 2017-08-15 17:53:23 +0000 (Tue, 15 Aug 2017) $
7# SVN revision $Rev: 906 $
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
19# push "the directory the script is in" into @INC
20use FindBin;
21use lib "$FindBin::RealBin/";
22
23use MyIPDB;
24
25my $ip_dbh;
26my $errstr;
27($ip_dbh,$errstr) = connectDB_My;
28if (!$ip_dbh) {
29 die "Database error: $errstr\n";
30}
31initIPDBGlobals($ip_dbh);
32
33my %userhash;
34my $passfile = "/var/www/ipdb.example.com/ip/.htpasswd";
35
36die ".htpasswd error: file is empty!\n"
37 if -z $passfile;
38
39die ".htpasswd error: file seems too small: ".(-s $passfile)."\n"
40 if (-s $passfile <3000);
41
42open HTPASS, "<$passfile";
43
44my $sth = $ip_dbh->prepare("select count(*) from users where username=?");
45my $sth2;
46while (<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')";
54 print "new user: $user\n";
55 } else {
56 $sql = "update users set password='$pass' where username='$user'";
57 }
58 $sth2 = $ip_dbh->prepare($sql);
59 $sth2->execute or print "error executing $sql: ".$DBI::errstr."\n";
60 $userhash{$user} = $pass;
61}
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;
67while (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 }
73}
Note: See TracBrowser for help on using the repository browser.