1

I created a bash file to register users in the ProFTPD Server. The bash file is run from a PHP file, sending 5 variables as parameters, which are: name, surname, user, password and a random number with 4 digits (eg. 7215)

Everytime I run it I get these errors and only users and their folders are created. The ProFTPD configuration is not changing.

#!/bin/bash

var1=$(echo $1 | tr [:upper:] [:lower:] | cut -c 1)
var2=$(echo $2 | tr [:upper:] [:lower:] | cut -c 1)

rnumber=$(echo $5)
dir=$(echo "dir")
folder=$(echo $var1$var2$var3$dir$rnumber)
user=$(echo $3)

sudo mkdir -p /var/www/ftpdir/Hostitec/usersftp/$folder

sudo chmod 777 /var/www/ftpdir/Hostitec/usersftp/$folder

command=$(cat -n /etc/proftpd/proftpd.conf | grep DefaultRoot | tail -n 1 | cut -f 1)

sudo head -n $command /etc/proftpd/proftpd.conf > /etc/proftpd/copyconf

echo "DefaultRoot   /var/www/ftpdir/Hostitec/usersftp/$folder $user" >> /etc/proftpd/copyconf

lastone=$(expr $command + 1)

sudo tail -n +$lastone /etc/proftpd/proftpd.conf >> /etc/proftpd/copyconf

sudo cat /etc/proftpd/copyconf > /etc/proftpd/proftpd.conf

sudo rm /etc/proftpd/copyconf

sudo useradd $user

sudo chown $user:$user /var/www/ftpdir/Hostitec/usersftp/$folder

echo "$user:$4" | sudo chpasswd -m

sudo service proftpd restart

checkdir=$(ls /var/www/ftpdir/Hostitec/usersftp/$folder | grep $folder)

checkuser=$(cat /etc/passwd | grep $user)

echo $checkdir > check.txt

echo $checkuser > check.txt

if ! [ -s check.txt ]
then
    rm -r /var/www/ftpdir/Hostitec/usersftp/$folder
    sudo userdel $user
fi

These are the errors I'm getting:

./script.sh: line 17: /etc/proftpd/copyconf: Permission denied
./script.sh: line 19: /etc/proftpd/copyconf: Permission denied
./script.sh: line 23: /etc/proftpd/copyconf: Permission denied
cat: /etc/proftpd/copyconf: No such file or directory
rm: You cannot remove '/etc/proftpd/copyconf': No such file or directory
0

2 Answers 2

0

The shell redirection is not being ran as root. Try replacing the redirections with tee

Replace these:

# Overwrite
sudo command > /path/file  # Original
sudo command | sudo tee /path/file  # Replacement

# Append
sudo command >> /path/file  # Original
sudo command | sudo tee -a /path/file  # Replacement

Another alternative is to run the entire script as root

2
  • So, If I run it as root, Do I need to place sudo at the start of every command?
    – Antonio
    Sep 5, 2018 at 12:40
  • @Antonio: no, you don't
    – 0x2b3bfa0
    Sep 5, 2018 at 13:28
0

There are a few problems with your script, for instance, in the line sudo cat /etc/proftpd/copyconf > /etc/proftpd/proftpd.conf only the sudo cat /etc/proftpd/copyconf is executed with super user privileges, the > /etc/proftpd/proftpd.conf part is executed with the privileges of the invoking user I think that the following changes to you script will resolve your issues.

    #!/bin/bash

var1=$(echo $1 | tr [:upper:] [:lower:] | cut -c 1)
var2=$(echo $2 | tr [:upper:] [:lower:] | cut -c 1)

rnumber=$(echo $5)
dir=$(echo "dir")
folder=$(echo $var1$var2$var3$dir$rnumber)
user=$(echo $3)

sudo mkdir -p /var/www/ftpdir/Hostitec/usersftp/$folder

sudo chmod 777 /var/www/ftpdir/Hostitec/usersftp/$folder

command=$(cat -n /etc/proftpd/proftpd.conf | grep DefaultRoot | tail -n 1 | cut -f 1)

sudo head -n $command /etc/proftpd/proftpd.conf > /tmp/copyconf

echo "DefaultRoot   /var/www/ftpdir/Hostitec/usersftp/$folder $user" >> /tmp/copyconf

lastone=$(expr $command + 1)

sudo tail -n +$lastone /etc/proftpd/proftpd.conf >> /tmp/copyconf

sudo cp /tmp/copyconf /etc/proftpd/proftpd.conf

sudo rm /tmp/copyconf

sudo useradd $user

sudo chown $user:$user /var/www/ftpdir/Hostitec/usersftp/$folder

echo "$user:$4" | sudo chpasswd -m

sudo service proftpd restart

checkdir=$(ls /var/www/ftpdir/Hostitec/usersftp/$folder | grep $folder)

checkuser=$(cat /etc/passwd | grep $user)

echo $checkdir > check.txt

echo $checkuser > check.txt

if ! [ -s check.txt ]
then
    rm -r /var/www/ftpdir/Hostitec/usersftp/$folder
    sudo userdel $user
fi
2
  • 2
    It would be helpful if you would point out the difference(s) between your script and the original one so people don't need to copy and diff them manually.
    – PerlDuck
    Sep 5, 2018 at 13:05
  • Good point -- I mainly changed the lines where the original script generated errors. the primary change to get past the problem with the redirects to the 'copyconf' files was to create the temporary file in /tmp so permissions wouldn't be a problem and change the 'cat /tmp/copyconf > /etc/proftpd/proftpd.conf' to a straight copy to avoid the redirect issue there.
    – B.McCready
    Sep 5, 2018 at 14:06

Not the answer you're looking for? Browse other questions tagged .