#! /bin/sh
# $Id: dnsmon_account,v 1.2.2.1 2008/04/13 15:49:48 ruben Exp $
############################################################################
###    (C)opyright 2003 - 2008 RIPE NCC
###    This file is part of DNSMon
###
###    DNSMon is free software: you can redistribute it and/or modify
###    it under the terms of the GNU General Public License as published by
###    the Free Software Foundation, either version 3 of the License, or
###    (at your option) any later version.
###
###    DNSMon is distributed in the hope that it will be useful,
###    but WITHOUT ANY WARRANTY; without even the implied warranty of
###    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
###    GNU General Public License for more details.
###
###    You should have received a copy of the GNU General Public License
###    along with DNSMon.  If not, see <http://www.gnu.org/licenses/>.
############################################################################

DB_HOST='database.example.net'
DB_USER='databaseuser'
DB_PASS='databasepass'
DB_NAME='databasename'

OPERATION=$1
DNSMONUSER=$2
DNSMONPASS=$3
SQL=""

if [ "X${1}" != "Xlist" ]; then
	if [ $# -lt 3 ]; then
			echo "`/usr/bin/basename $0`: <add|delete|change|list> <dnsmon_user> <dnsmon_password>"
			exit 69
	fi
fi

case $OPERATION in
	add)
		SQL="INSERT INTO Users (logname, password) VALUES ('$DNSMONUSER', '$DNSMONPASS');"
		;;
	delete)
		SQL="UPDATE Users SET password='$DNSMONPASS' WHERE logname='$DNSMONUSER'"
		;;
	change)
		SQL="DELETE FROM Users WHERE logname='$DNSMONUSER'"
		;;
	list)
		SQL="SELECT * FROM Users ORDER BY logname"
		;;
	*)
		echo "`/usr/bin/basename $0`: <add|delete|change> <dnsmon_user> <dnsmon_password>"
		exit 69
		;;
esac

        cat <<EOT  | mysql -h "${DB_HOST}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}"
$SQL
EOT

if [ $? -gt 0 ]; then
	echo "`/usr/bin/basename $0`  Unable to $OPERATION for $DNSMONUSER"
else
	echo "$OPERATION $DNSMONUSER completed"
fi

