#!/usr/bin/perl my $ID = q$Id: check_quotas.pl,v 1.2 2003/12/21 17:39:53 pkremer Exp $; # # check_quotas.pl -- Validate and recompute pure-ftpd quotas based on mysql user data # # Written by Paul Kremer # Copyright 2003, Paul Kremer. # # This program is free software; you can redistribute it and/or modify it # under the same terms as Perl itself. ############################################################################## # Site configuration ############################################################################## use strict; use warnings; use DBI; use Getopt::Long qw(GetOptions); # Clean up $0 for error reporting. my $fullpath = $0; $0 =~ s%^.*/%%; # command line arguments my ($help, $version, $configfile) = (undef, undef, undef); Getopt::Long::config ('bundling', 'no_ignore_case'); GetOptions ('help|h' => \$help, 'version|v' => \$version, 'config|c=s' => \$configfile) or exit 1; if ($help) { print "Feeding myself to perldoc, please wait....\n"; exec ('perldoc', '-t', $fullpath) or die "$0: can't fork: $!\n"; } elsif ($version) { my $version = join (' ', (split (' ', $ID))[1..3]); $version =~ s/,v\b//; $version =~ s/(\S+)$/($1)/; print $version, "\n"; exit 0; } unless ( $configfile ) { $configfile = '/etc/spu.conf'; } my $prefs = readPreferences($configfile); die "$0: config key 'pureftpd_quotacheck' missing\n" unless defined $prefs->{pureftpd_quotacheck}; die "$0: config key 'pureftpd_database' missing\n" unless defined $prefs->{pureftpd_database}; die "$0: config key 'pureftpd_db_hostname' missing\n" unless defined $prefs->{pureftpd_db_hostname}; die "$0: config key 'pureftpd_db_username' missing\n" unless defined $prefs->{pureftpd_db_username}; die "$0: config key 'pureftpd_db_password' missing\n" unless defined $prefs->{pureftpd_db_password}; if (! -e $prefs->{pureftpd_quotacheck} ) { die "$0: $prefs->{pureftpd_quotacheck} could not be found\n"; }; my $dbh = DBI->connect("DBI:mysql:$prefs->{pureftpd_database}:$prefs->{pureftpd_db_hostname}", $prefs->{pureftpd_db_username}, $prefs->{pureftpd_db_password}) || die "$0: Cannot connect to MySQL server: DBI:err\n"; ##################################################################### sub check_quotas { my $statement ="SELECT Dir,Uid,Gid FROM users"; my $sth = $dbh->prepare($statement) or die "$0: Can't prepare $statement: $dbh->errstr\n"; $sth->execute; my @row; while(@row = $sth->fetchrow_array) { my $Dir = $row[0]; my $Uid = $row[1]; my $Gid = $row[2]; if ( -e "$Dir" ) { my @command = ('nice','-n','20',$prefs->{pureftpd_quotacheck},'-u',$Uid,'-g',$Gid,'-d',$Dir); if (system(@command) != 0) { my $exit_value = $? >> 8; my $signal_num = $? & 127; my $dumped_core = $? & 128; warn "$0: system @command failed: $?\n"; warn "$0: exit value: $exit_value, signal num: $signal_num, dumped core: $dumped_core\n"; warn "continuing...\n"; } } else { warn "$0: $Dir does not exist\n"; }; }; }; ##################################################################### sub readPreferences { my $rc = shift || die "$0: no config file specified\n"; my $result = undef; if (-f $rc) { open (RC,"<$rc") || warn("$0: error opening '$rc': $!\n"); while () { my $line = $_; next if $line =~ /^\s*[#%;]/ or $line =~ /^\s*\r*$/; if ($line =~ m/^\s*([^\s=]+)\s*=\s*(.*?)\s*$/) { my ($key, $value) = ($1, $2); die "$0: duplicate cfg value: $key\n" if exists $result->{$key}; $result->{$key} = $value; } else { die "TODO '$line' in '".$rc."'"; } } } else { die "$0: config file $rc not found\n"; } return $result; }; ####################### MAIN ###################################### check_quotas();