eComEvo
eComEvo

Reputation: 12599

Shell script to install and configure SFTP server on Ubuntu is broken

I'm trying to create a shell script to automate setup of an SFTP server, but I've run into some problems as noted in the code.

#!/bin/bash

sftp_user='sftpuser'
sftp_passwd='SomePassword'

print_success() { echo -e "\e[32m${@}\e[0m"; }
print_error() { echo -e "\e[31m${@}\e[0m"; }

if [ `whoami` != "root" ] && [ `whoami` != "forge" ] && [ `whoami` != "homestead" ] && [ `whoami` != "vagrant" ];
then
    print_error "You must be root to run this script"
    exit 1
fi

# PROBLEM #1 The IF below doesn't correctly detect when apt is updated. Always says failed.
echo "Updating apt"
sudo apt-get update 2>&1
if [ $? -ne 0 ]
then
    print_success "Updated apt"
else
    print_error "Failed to update apt"
    exit 1
fi

echo "Installing VsFTPD package"
sudo apt-get -y install vsftpd 2>&1
if [ $? -ne 0 ]
then
    print_success "Installed VsFTPD"
else
    print_error "Failed to Install VsFTPD"
    exit 1
fi

# PROBLEM #2 The conditional below does not detect if openssh server already installed and up to date.
echo "Installing openssh-server"
sudo apt-get install openssh-server 2>&1
if [ $? -ne 0 ]
then
    print_success "Installed openssh-server"
else
    print_error "Failed to Install openssh-server"
    exit 1
fi

sshd_config='/etc/ssh/sshd_config'

sudo mv $sshd_config $sshd_config.bak
sudo rm -f $sshd_config

echo "# SSH Config
Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
UsePrivilegeSeparation yes
KeyRegenerationInterval 3600
ServerKeyBits 1024
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 120
PermitRootLogin without-password
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
IgnoreRhosts yes
RhostsRSAAuthentication no
HostbasedAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
AcceptEnv LANG LC_*
UsePAM yes
Subsystem sftp internal-sftp
Match group ftpaccess
ChrootDirectory %h
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
" | sudo tee $sshd_config

echo "Restarting SSH service"
sudo service ssh restart

echo "Creating user and group"
sudo groupadd ftpaccess
sudo useradd -m $sftp_user -g ftpaccess -s /usr/sbin/nologin
echo "$sftp_passwd" | sudo passwd $sftp_user

echo "Done"

I'm no pro at making shell scripts, so it's quite possible I missed something else, too. Any ideas?

Code is based on: http://www.krizna.com/ubuntu/setup-ftp-server-on-ubuntu-14-04-vsftpd/

EDIT: The updated script with all fixes in case anyone is interested:

#!/bin/bash

# Based on: http://www.krizna.com/ubuntu/setup-ftp-server-on-ubuntu-14-04-vsftpd/
sftp_user='someuser'
sftp_passwd='SomePassword'

# IMPORTANT: Edit the values above or comment out the user add code before running.

print_success() { echo -e "\e[32m${@}\e[0m"; }
print_error() { echo -e "\e[31m${@}\e[0m"; }

if [ `whoami` != "root" ] && [ `whoami` != "forge" ] && [ `whoami` != "homestead" ] && [ `whoami` != "vagrant" ];
then
    print_error "You must be root to run this script!"
    exit 1
fi

echo "Updating apt..."
sudo_output=$(sudo bash -c "apt-get update 2>&1; echo $?")
sudo_result=$?
aptget_result=$(echo "${sudo_output}"| tail -1)

echo "${sudo_output}"

# Check results
if [ ${sudo_result} -eq 0 ]; then
    if [ ${aptget_result} -eq 0 ]; then
       print_success "Updated apt."
    else
       print_error "Failed to apt, apt-get error!"
    fi
else
    print_error "Failed to update apt, sudo error!"
    exit 1
fi

echo "Installing VsFTPD package..."
sudo_output=$(sudo bash -c "apt-get -y install vsftpd 2>&1; echo $?")

# Get results.
sudo_result=$?
aptget_result=$(echo "${sudo_output}"| tail -1)

# Show apt-get output.
echo "${sudo_output}"

# Check results
if [ ${sudo_result} -eq 0 ]; then
    if [ ${aptget_result} -eq 0 ]; then
       print_success "Installed VsFTPD."
    else
       print_error "Failed to Install VsFTPD, apt-get error!"
    fi
else
    print_error "Failed to Install VsFTPD, sudo error!"
    exit 1
fi

echo "Installing openssh-server"
sudo_output=$(sudo bash -c "apt-get install openssh-server 2>&1; echo $?")
sudo_result=$?
aptget_result=$(echo "${sudo_output}"| tail -1)
echo "${sudo_output}"

# Check results
if [ ${sudo_result} -eq 0 ]; then
    if [ ${aptget_result} -eq 0 ]; then
       print_success "Installed openssh-server."
    else
       print_error "Failed to install openssh-server, apt-get error!"
    fi
else
    print_error "Failed to install openssh-server, sudo error!"
    exit 1
fi

sshd_config='/etc/ssh/sshd_config'

sudo mv $sshd_config $sshd_config.bak
sudo rm -f $sshd_config

echo "# SSH Config
Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
UsePrivilegeSeparation yes
KeyRegenerationInterval 3600
ServerKeyBits 1024
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 120
PermitRootLogin without-password
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
IgnoreRhosts yes
RhostsRSAAuthentication no
HostbasedAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
AcceptEnv LANG LC_*
UsePAM yes
Subsystem sftp internal-sftp
Match group ftpaccess
ChrootDirectory %h
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
" | sudo tee $sshd_config

echo "Restarting SSH service..."
sudo service ssh restart

echo "Creating user and group..."
sudo groupadd ftpaccess
sudo useradd -m $sftp_user -g ftpaccess -s /usr/sbin/nologin
echo "$sftp_passwd" | sudo passwd $sftp_user

echo "Done! :)"

Upvotes: 0

Views: 1175

Answers (1)

Walter A
Walter A

Reputation: 20032

You are checking the return value of sudo, not the return value of apt-get.

EDIT:
sudo "whoami; whoami" will return an error, you need sudo bash -c 'whoami; whoami'. The solution was without bash -c, I changed this.
Note: Perhaps the whole construction is not needed, sudo true; echo $?; sudo false; echo $? returns 0 and 1 on my server. Can you check the output of your sudo command?

When you want to check both:

sudo_output=$(sudo bash -c "apt-get -y install vsftpd 2>&1; echo $?")
# Get results
sudo_result=$?
aptget_result=$(echo "${sudo_output}"| tail -1)
# Show apt-get output
echo "${sudo_output}"

# check results

if [ ${sudo_result} -eq 0 ]; then
    if [ ${aptget_result} -eq 0 ]; then
       print_success "Installed VsFTPD"
    else
       print_error "Failed to Install VsFTPD, apt-get error"
    fi
else
    print_error "Failed to Install VsFTPD, sudo error"
    exit 1
fi

Upvotes: 1

Related Questions