#!/usr/bin/perl -w

######################################################################
#
# Allowe or disallowe users to tcp quota...
# 
#
# 1996-09-15 turbo: begun
# 1996-09-16 turbo: changed so that database 'tcpquota' contain all info
# 1996-09-23 turbo: adding 0 quota in the database when adding a NEW user
#
# $Header: /usr/local/src/cvsroot/tcpquota/addtcpquota,v 1.10 1998/03/31 12:16:02 turbo Exp $
#
# $Log: addtcpquota,v $
# Revision 1.10  1998/03/31 12:16:02  turbo
# Make sure we search and opens the correct config file,
# set by the variables '{lib|conf}_dir' at the top...
#
# Revision 1.9  1997/12/03 10:14:55  turbo
# * Removed some fucked up CVS header lines...
# * Rewrote the command line handling a little, more GNU like...
# * Double check that we have the arguments required, before accessing the
#   database.
# * Moved the 'list_dbase()' to the library file, it was (practicaly) used
#   by the 'chktcpquota' script to...
# * If the user exist in the database, output/exit with 1, otherwise 0.
# * Added a 'help()' function, which describes the usage...
#
# Revision 1.8  1997/11/26 21:29:54  turbo
# Removed a lot of config file variables, that could possibly confuse a new
# user/admin of the package... We asume that whoever chooses to install the
# package, use our default... If not, they can go in and change the stuff
# themselvs!!
#
# Revision 1.7  1997/11/04 14:53:32  turbo
# We should require './lib/tcpquota.pl' instead of 'lib/tcpquota.pl'...
#
# Revision 1.6  1997/09/26 15:52:57  turbo
# * Forgot the $DEBUG variable... Perl complained...
# * Better looking output if started without parameters... We have to understand
#   that the 'lst' param takes param <user>... ('[lst <user>]' instead of
#   '[lst] <user>')
#
# Revision 1.5  1997/08/17 17:19:19  turbo
# * Added the cvs Header and Log
# * Removed a lot of config file variables, that could possibly confuse a new
# * user/admin of the package... We asume that whoever chooses to install the
# * package, use our default... If not, they can go in and change the stuff
# * themselvs!!
#
# * Moved some functions to the library file.
# * Deleted some variables, they exists in the config file.
#
#

# Include some magic...
use Msql;
package main;
#use Getopt::Long;

$lib_dir  = "./lib";
$conf_dir = "./confs";

require "$lib_dir/tcpquota.pl";

$ADD   = 0; $REM = 0; $CHK = 0; $LST = 0;
$dummy = 0;

# Does we have any arguments?
if( $ARGV[0] ) {
    # Ohh yes.... Process it...

    foreach $arg (@ARGV) {
	# Help?
	if( $arg eq '--help' || $arg eq '?' || $arg eq '-h' ) {
	    &help();
	}

	# Check if user exist...
	if( $arg eq 'all' || $arg eq '--all' || $arg eq '-A' ) {
	    $CHK = 1;
	}

	# Add user?
	if( $arg eq 'add' || $arg eq '--add' || $arg eq '-a' ) {
	    $ADD = 1;
	}

	# Remove user?
	if( $arg eq 'rem' || $arg eq '--rem' || $arg eq '-r' ) {
	    $REM = 1;
	}

	# List?
	if( $arg eq 'lst' || $arg eq '--lst' || $arg eq '-l' ) {
	    $LST = 1;
	}
    }
} else {
    &help();
}

if( $LST != 1 ) {
    $USER = $ARGV[1];
}

######################################################################
#
# configuration parameters
#

$PROG="addtcpquota";
$CF_FILE="tcpquota.cf";

%cf=(); # config array.
&readconfig("$conf_dir/$CF_FILE",$PROG);

######################################################################
#
# initializing stuff
#

# Open up the msql connection...
$dbh = Msql->connect( $cf{'SERVER'}, "tcpquota")
    || die( "Can't connect to msqld." );

######################################################################
#
# main loop
#
if( $ADD == 1 ) {
    if( $USER ) {
	&write_dbase($USER,'add');
    } else {
	printf("Use $0 add <user>\n");
    }
    exit(0);
}
if( $REM == 1 ) {
    if( $USER ) {
	&write_dbase($USER,'rem');
    } else {
	printf("Use $0 rem <user>\n");
    }
    exit(0);
}
if( $CHK == 1 ) {
    if( $USER ) {
	&read_dbase($USER);
    } else {
	printf("Use $0 all <user>\n");
    }
    exit(0);
}
if( $LST == 1 ) {
    &list_dbase("allowed");
    exit(0);
}

######################################################################
#
# write_dbase( $user )
#
# Add the user to the database...
#
sub write_dbase {
    local($user,$action) = @_;

    # Add the user in the allowed database...
    if( $action eq 'add' ) {
	$query = "insert into allowed (name) values ('$user')";
    } else {
	$query = "delete from allowed where name like '$user'";
    }
    $sth   = $dbh->query($query)
	|| die( "Error in updating allowed database." );

    # Add 0 in the quota database (if he/she doesn't exist there already)
    if( $action eq 'add' ) {
	# Check if the user already exist in the database...
	$sth = $dbh->query("select * from tcptab where name like '$user'")
	    || print "Error when query...\n";

	($dummy,$sec) = $sth->fetchrow;

	if(!$sec) {
	    $sth   = $dbh->query("insert into tcptab (name,quota) values ('$user',0)")
		|| die( "Error in updating quota database.");
	}
    }

    # Print some info about the actions...
    if( $action eq 'add' ) {
	print "Added user `$user'...\n";
    } else {
	print "Removed user `$user'...\n";
    }
}

######################################################################
#
# read_dbase( $user )
#
# Check if the user is in the database...
#
sub read_dbase {
    local($user) = @_;

    # Query the database.
    $sth = $dbh->query("select * from allowed where name like '$user'")
	|| terminate( "Error when query..." );

    ($name) = $sth->fetchrow;

    if( $name ) {
	# User is allowed...
	print "1";
	exit 1;
    } else {
	# User is not allowed...
	print "0";
	exit 0;
    }
}

######################################################################
#
# help( void )
#
# Print out some help and then exit
#
sub help {
    print "Use: $0 [[all] [add] [rem] <user>] [lst]\n";
    print "     $0 [[--all] [--add] [--rem] <user>] [--lst]]\n";
    print "     $0 [[-A] [-a] [-r] [<user>]] [-l]\n";
    print "     (or any combination there of...)\n";
    exit -1;
}
