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
Antonio
  • 21

2 Answers2

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

0x2b3bfa0
  • 9,110
  • 7
  • 38
  • 55
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