1
1
scripts/addaccount.sh

216 lines
5.4 KiB
Bash
Raw Normal View History

2010-06-26 02:43:02 +02:00
#!/bin/bash
2010-06-26 03:01:46 +02:00
############################################
## ##
2010-06-26 03:07:42 +02:00
## FTP/sFTP Account Creation Script ##
2011-01-05 11:06:01 +01:00
## v0.3 ##
2010-06-26 03:07:42 +02:00
## Author: david@socialnerds.org ##
2010-06-26 03:01:46 +02:00
## ##
############################################
## script configuration section ##
2011-01-05 11:06:01 +01:00
accpath="/srv/storage"
quotamountpoint="/srv/storage"
2010-06-26 03:07:42 +02:00
acchost="some.domain.org" # the dns name where your sever is reachable
2010-06-26 03:01:46 +02:00
sftpgroup="sftpusers" # this group must exist
trackrequester="yes" # switch to "no" if you do not want to track the requester
logging=1 # set this to 0 if you don't want any logging
logpath="/media/storage/logs" # there you want to create your logfile
logfile="accounts.log" # choose the logfile name here
2011-01-05 11:06:01 +01:00
jabberlog=0 # set this to 0 if you don't want jabber notifications
maillog=0
2010-06-26 03:01:46 +02:00
## following ist not necessary if jabberlog=0
2010-06-26 03:07:42 +02:00
jabberwatchdogs="admin@somedomain.org admin@someotherdomain.org"
jabberuser="jabber-account"
jabberserver="jabber-server"
jabberpass="jabber-account-password"
2010-06-26 02:43:02 +02:00
2011-01-05 11:06:01 +01:00
## do not touch
version="v0.3"
2010-06-26 02:43:02 +02:00
2010-06-26 03:01:46 +02:00
## am i root? ##
if [ "$(whoami)" != "root" ]; then
2010-06-26 02:43:02 +02:00
echo "only root can do this"
exit 1;
fi
2010-06-26 03:01:46 +02:00
## check for dependencys ##
2010-06-26 02:43:02 +02:00
# not yet implemented (sendxmpp, ssh, vsftpd, ..)
2010-06-26 03:01:46 +02:00
#clear
echo "" # just an empty line
2011-01-05 11:06:01 +01:00
echo "Welcome to the FTP/sFTP Account Creation Script ($version)"
2010-06-26 02:43:02 +02:00
## choose ftp or sftp
2010-06-26 03:01:46 +02:00
echo ""
echo "Which type of account you want to create? [sftp|ftp]"
2010-06-26 02:43:02 +02:00
read acctype
if [ $acctype = "sftp" ]; then
2011-01-05 11:06:01 +01:00
:
2010-06-26 02:43:02 +02:00
else
if [ $acctype = "ftp" ]; then
2011-01-05 11:06:01 +01:00
:
2010-06-26 02:43:02 +02:00
else
#clear
echo "I'm sorry, i need to break this up right now."
echo "It seams you can't understand some simple instructions.."
exit 1;
fi
fi
2010-06-26 03:01:46 +02:00
if [ -z $1 ]; then
needaccname="yes"
while [ $needaccname = "yes" ]; do
echo ""
echo "Enter Accountname:"
read accname
if [ -z $accname ]; then
echo "This field is mandatory."
else
needaccname="notanymore"
fi
done
else
accname=$1
fi
2010-06-26 02:43:02 +02:00
## quota
2011-01-05 11:06:01 +01:00
ablocks=$(df | grep $quotamountpoint | awk '{print $2}')
gblocks=$(repquota $quotamountpoint | grep 000 | awk '{print $4}')
set -- $gblocks
quotacount=0
for var in "$@"
do
quotacount=$(($quotacount+$var))
done
gblocks=$quotacount
fblocks=$(($ablocks-$gblocks))
fsize=${fblocks:0:$((${#fblocks}-3))}
needaccquota="yes"
while [ $needaccquota = "yes" ]; do
echo ""
echo "Please specify how much diskspace this account should provide. (in Megabytes)"
echo "Maximum: $fsize"
read accquota
if [ -z "$accquota" ]; then
echo "This field is mandatory."
else
if [ $accquota -lt $fsize ]; then
needaccquota="notanymore"
else
echo ""
echo "Specified size too big or not a number. Try again."
fi
fi
done
2010-06-26 02:43:02 +02:00
2010-06-26 03:01:46 +02:00
2010-06-26 02:43:02 +02:00
## requester
2010-06-26 03:01:46 +02:00
while [ $trackrequester = "yes" ]; do
echo ""
echo "Who orderd this account? (I'm tracking this for a greater good.)"
read accrequester
if [ -z "$accrequester" ]; then
echo "This field is mandatory."
else
trackrequester="notanymore"
fi
done
2010-06-26 02:43:02 +02:00
## set $accport
if [ $acctype = "sftp" ]; then
accport="22"
else
accport="21"
fi
2010-06-26 03:01:46 +02:00
2010-06-26 02:43:02 +02:00
## get timestamp
2011-01-05 11:06:01 +01:00
acctimestamp=$(date '+%d.%m.%Y %H:%M')
2010-06-26 02:43:02 +02:00
2010-06-26 03:01:46 +02:00
## gen password (acpass)
accpass=$(pwgen -snc 10 1)
echo $accpass > pass.txt
accencpass=$(makepasswd --clearfrom=pass.txt --crypt-md5 |awk '{print $2}')
rm pass.txt
2010-06-26 02:43:02 +02:00
## create home, set its permissions and add the user to sftp/ftpgroup
if [ $acctype = "sftp" ]; then
mkdir -p $accpath/$acctype"_accounts"/$accname/data
# create the actual user (sftp)
2010-06-26 03:01:46 +02:00
useradd -d /data -M -U -s /usr/lib/sftp-server -p $accencpass $accname
2010-06-26 02:43:02 +02:00
chown -R $accname\: $accpath/$acctype"_accounts"/$accname/data
2011-01-05 11:06:01 +01:00
usermod -G $sftpgroup $accname
2010-06-26 02:43:02 +02:00
else
mkdir -p $accpath/$acctype"_accounts"/$accname
# create the actual user (ftp)
2010-06-26 03:01:46 +02:00
useradd -d $accpath/$acctype"_accounts"/$accname -M -U -s /bin/false -p $accencpass $accname
2010-06-26 02:43:02 +02:00
chown -R $accname\: $accpath/$acctype"_accounts"/$accname
2010-06-26 03:01:46 +02:00
echo $accname >> /etc/vsftpd.user_list
2010-06-26 02:43:02 +02:00
fi
2011-01-05 11:06:01 +01:00
## configure quota
setquota --all -u $accname $accquota"000" $accquota"000" 0 0
2010-06-26 03:01:46 +02:00
2010-06-26 02:43:02 +02:00
## logging (log type, name, pass, quota, requester and timestamp)
if [ $logging = 1 ]; then
if [ -e $logpath/$logfile ]; then
cd $logpath
echo $acctype $accname $accpass $accquota $accrequester $acctimestamp >> $logfile
else
mkdir -p $logpath
cd $logpath
touch $logfile
echo "type name pass quota reguester timestamp" >> $logfile
echo $acctype $accname $accpass $accquota $accrequester $acctimestamp >> $logfile
fi
else
echo ""
fi
## jabber notification
if [ $jabberlog = 1 ]; then
echo "
2010-06-26 03:07:42 +02:00
This is your FTP/sFTP Server,
2010-06-26 02:43:02 +02:00
a "$acctype" account was just created.
Accountname:" $accname "
2011-01-05 11:06:01 +01:00
Quota:" $accquota"MB" "
2010-06-26 03:01:46 +02:00
Requester:" $accrequester | sendxmpp -r ftpcreation -u $jabberuser -j $jabberserver -p $jabberpass $jabberwatchdogs
2011-01-05 11:06:01 +01:00
fi
## mail notification
if [ $maillog = 1 ]; then
echo "mail notification is not yet implemented"
2010-06-26 02:43:02 +02:00
fi
## account data output
2010-06-26 03:01:46 +02:00
#clear
2010-06-26 02:43:02 +02:00
echo ""
echo "Account data"
echo ""
echo "Host:" $acchost
echo "Port:" $accport
echo "Username:" $accname
echo "Password:" $accpass
2011-01-05 11:06:01 +01:00
echo "Quota:" $accquota"MB"
2010-06-26 02:43:02 +02:00
echo "Directory:" $accpath/$acctype"_accounts"/$accname
echo "Requester:" $accrequester
echo ""
echo "Everything is done"
2010-06-26 03:01:46 +02:00
exit 0