daily pastebin goal
62%
SHARE
TWEET

Untitled

a guest Jan 25th, 2016 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. #------------------------------------------------------------------------------------
  4. # Initialize some variables
  5. #------------------------------------------------------------------------------------
  6. SHELL=/sbin/nologin
  7. FTPCONF=/etc/vsftpd
  8. HOMEDIR=/var/www
  9.  
  10.  
  11. if [ -f $FTPCONF/password ];then
  12.     ACCOUNTDB_TOTALLINES=`grep '.' -c $FTPCONF/password`
  13. else
  14.     ACCOUNTDB_TOTALLINES=0
  15. fi
  16.  
  17. function checkNewUser_Existence () {
  18.     C=1;
  19.  
  20.     if [ "$ACCOUNTDB_TOTALLINES" != "0" ];then
  21.         while [ $C -lt $ACCOUNTDB_TOTALLINES ]; do
  22.             VALIDUSER=`sed -n -e "$C p" $FTPCONF/password`
  23.             if [ "$USERNAME" == "$VALIDUSER" ];then
  24.                 USERNAMEOK=NO
  25.                 break;
  26.             else
  27.                 USERNAMEOK=YES
  28.            fi
  29.            let C=$C+2;
  30.         done
  31.     fi
  32. }
  33.  
  34. function checkNewUser_Availability () {
  35.  
  36.     if [ -f $FTPCONF/denied_users ];then
  37.         if [ ! `grep -w $USERNAME $FTPCONF/denied_users` ];then
  38.             USERNAMEOK=YES
  39.     else
  40.         USERNAMEOK=NO
  41.         fi
  42.  
  43.     else
  44.         USERNAMEOK=NO
  45.     fi
  46. }
  47.  
  48. function checkNewUser_Homedir () {
  49.  
  50.     # Verify User's Home Directory.
  51.     if [ -d $HOMEDIR ];then
  52.         for i in `ls $HOMEDIR/`; do
  53.            VALIDUSER=$i
  54.            if [ "$USERNAME" == "$VALIDUSER" ];then
  55.                USERNAMEOK=NO
  56.            break;
  57.        else
  58.            USENAMEOK=YES
  59.            fi
  60.         done
  61.     fi
  62. }
  63.  
  64. function getUsername () {
  65.  
  66.     printf " Enter Username (lowercase)      : "
  67.     read USERNAME
  68.  
  69.     checkNewUser_Existence;
  70.     checkNewUser_Availability;
  71.     checkNewUser_Homedir;
  72.  
  73.     if [ "$USERNAMEOK" == "NO" ];then
  74.         echo "  --> Invalid ftp virtual user. Try another username."
  75.         getUsername;
  76.     fi
  77.  
  78. }
  79.  
  80. #------------------------------------------------------------------------------------
  81. # Add some presentation :)
  82. #------------------------------------------------------------------------------------
  83. clear;
  84. echo '-------------------------------------------------------------------'
  85. echo " vsftpd -> Virtual Users -> Add Virtual User"
  86. echo '-------------------------------------------------------------------'
  87.  
  88. # Check dependencies
  89. PACKISMISSING=""
  90. PACKDEPENDENCIES="vsftpd libdb4-utils"
  91. for i in `echo $PACKDEPENDENCIES`; do
  92.     /bin/rpm -q $i > /dev/null
  93.     if [ "$?" != "0" ];then
  94.         PACKISMISSING="$PACKISMISSING $i"
  95.     fi
  96. done
  97. if [ "$PACKISMISSING" != "" ];then
  98.     echo " ATTENTION: The following package(s) are needed by this script:"
  99.     for i in `echo $PACKISMISSING`; do
  100.         echo "             - $i"
  101.     done
  102.     echo '-------------------------------------------------------------------'
  103.     exit;
  104. fi
  105.  
  106.  
  107. #
  108. # Get user information
  109. #
  110. getUsername;
  111. printf " Enter Password (case sensitive) : "
  112. read PASSWORD
  113. printf " Enter Comment(user's full name) : "
  114. read FULLNAME
  115. printf " Account disabled ? (y/N)        : "
  116. read USERSTATUS
  117. echo " Home directory location         : ${HOMEDIR}/$USERNAME "
  118. echo " Home directory permissions      : $USERNAME.$USERNAME | 750 | public_content_rw_t"
  119. echo " Login Shell                     : $SHELL "
  120.  
  121. #
  122. # Create specific user configuration
  123. #
  124. echo "dirlist_enable=YES
  125. download_enable=YES
  126. local_root=/var/www/$USER
  127. write_enable=YES" > /etc/vsftpd/user_conf/$USERNAME
  128.  
  129. #
  130. # Update denied_users file
  131. #
  132. if [ "$USERSTATUS" == "y" ];then
  133.     echo $USERNAME >> $FTPCONF/denied_users
  134. else
  135.     sed -i -r -e "/^$USERNAME$/ d" $FTPCONF/denied_users
  136. fi
  137.  
  138. #Create user
  139. echo $USERNAME | tee /etc/vsftpd/password{,-nocrypt} > /dev/null
  140.  
  141. #Update password.db file
  142. mypass=$PASSWORD
  143. echo $mypass >> /etc/vsftpd/password-nocrypt
  144. echo $(openssl passwd -crypt $mypass) >> /etc/vsftpd/password
  145. db_load -T -t hash -f $FTPCONF/password $FTPCONF/password.db
  146.  
  147. # Create ftp virtual user $HOMEDIR
  148. if [ ! -d $HOMEDIR  ];then
  149.     mkdir $HOMEDIR
  150. fi
  151.  
  152.  
  153. # Create home directory
  154. mkdir -p $HOMEDIR/$USERNAME
  155.  
  156. # Set Permissions
  157. chmod 600 $FTPCONF/password.db
  158. chmod 750 $HOMEDIR/$USERNAME
  159. chown -R vsftpd:vsftpd $HOMEDIR
  160.  
  161.  
  162. # Restart vsftpd after user addition.
  163. echo '-------------------------------------------------------------------'
  164. /sbin/service vsftpd reload
  165. echo '-------------------------------------------------------------------'
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top