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

Last change on this file since 919 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
RevLine 
[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
12use strict;
13use warnings;
14use 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
20use FindBin;
21use lib "$FindBin::RealBin/";
22
[373]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;
[399]34my $passfile = "/var/www/ipdb.example.com/ip/.htpasswd";
[373]35
[399]36die ".htpasswd error: file is empty!\n"
37 if -z $passfile;
[373]38
[399]39die ".htpasswd error: file seems too small: ".(-s $passfile)."\n"
40 if (-s $passfile <3000);
41
42open HTPASS, "<$passfile";
43
[373]44my $sth = $ip_dbh->prepare("select count(*) from users where username=?");
[399]45my $sth2;
[373]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')";
[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;
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 }
[373]73}
Note: See TracBrowser for help on using the repository browser.