#!/bin/bash ############################################ ## ## ## FTP/sFTP Account Creation Script ## ## v0.3 ## ## Author: david@socialnerds.org ## ## ## ############################################ ## script configuration section ## accpath="/srv/storage" quotamountpoint="/srv/storage" acchost="some.domain.org" # the dns name where your sever is reachable 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 jabberlog=0 # set this to 0 if you don't want jabber notifications maillog=0 ## following ist not necessary if jabberlog=0 jabberwatchdogs="admin@somedomain.org admin@someotherdomain.org" jabberuser="jabber-account" jabberserver="jabber-server" jabberpass="jabber-account-password" ## do not touch version="v0.3" ## am i root? ## if [ "$(whoami)" != "root" ]; then echo "only root can do this" exit 1; fi ## check for dependencys ## # not yet implemented (sendxmpp, ssh, vsftpd, ..) #clear echo "" # just an empty line echo "Welcome to the FTP/sFTP Account Creation Script ($version)" ## choose ftp or sftp echo "" echo "Which type of account you want to create? [sftp|ftp]" read acctype if [ $acctype = "sftp" ]; then : else if [ $acctype = "ftp" ]; then : 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 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 ## quota 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 ## requester 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 ## set $accport if [ $acctype = "sftp" ]; then accport="22" else accport="21" fi ## get timestamp acctimestamp=$(date '+%d.%m.%Y %H:%M') ## 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 ## 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) useradd -d /data -M -U -s /usr/lib/sftp-server -p $accencpass $accname chown -R $accname\: $accpath/$acctype"_accounts"/$accname/data usermod -G $sftpgroup $accname else mkdir -p $accpath/$acctype"_accounts"/$accname # create the actual user (ftp) useradd -d $accpath/$acctype"_accounts"/$accname -M -U -s /bin/false -p $accencpass $accname chown -R $accname\: $accpath/$acctype"_accounts"/$accname echo $accname >> /etc/vsftpd.user_list fi ## configure quota setquota --all -u $accname $accquota"000" $accquota"000" 0 0 ## 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 " This is your FTP/sFTP Server, a "$acctype" account was just created. Accountname:" $accname " Quota:" $accquota"MB" " Requester:" $accrequester | sendxmpp -r ftpcreation -u $jabberuser -j $jabberserver -p $jabberpass $jabberwatchdogs fi ## mail notification if [ $maillog = 1 ]; then echo "mail notification is not yet implemented" fi ## account data output #clear echo "" echo "Account data" echo "" echo "Host:" $acchost echo "Port:" $accport echo "Username:" $accname echo "Password:" $accpass echo "Quota:" $accquota"MB" echo "Directory:" $accpath/$acctype"_accounts"/$accname echo "Requester:" $accrequester echo "" echo "Everything is done" exit 0