Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Host Management Module #66

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
172 changes: 172 additions & 0 deletions libraries/module/manage-host.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#!/bin/bash
# Common Functions For Module: Manage Host

# Module Functions
manage-host() {
#####################
## Check Functions ##
#####################

# Check If Array Empty
manage-host-check-array() {
if [ $1 = 0 ]; then
# Print Message
error "No hosts in host array. Aborting."

# Exit Loop
break
fi
}

# Check If User HTTP Directory Exists
manage-host-check-http() {
if [ ! -d /home/$1/http ]; then
# Print Message
echo "User does not have HTTP directory ($1)."

# Continue Loop
continue
fi
}

# Check If User Exists
manage-host-check-host() {
if [ ! -f /etc/nginx/sites-available/$1-$2.conf ]; then
# Print Message
echo "Invalid host ($2)."

# Continue Loop
continue
fi
}

###########################
## Interactive Functions ##
###########################

# Input Check
manage-host-input-check() {
# Check Loop
while true; do
# Take Input
read -p "Please enter a host: " HOST

# Check Input
if [ -f /etc/nginx/sites-available/$1-$HOST.conf ]; then
# Exit Loop
break
else
# Print Error
echo "Invalid host. Ensure the host exists on the system."
fi
done
}

# Input Host
manage-host-input-host() {
# Check Loop
while true; do
# Take Input
read -p "Please enter a host: " HOST

# Check Input
if egrep -q '^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$' <<< $HOST; then
# Exit Loop
break
else
# Print Error
echo "Invalid host. Ensure the hostname is of valid format."
fi
done
}

##########################
## Management Functions ##
##########################

# Add Host
manage-host-manage-add() {
subheader "Creating Host Directory..."
mkdir /home/$1/http/hosts/$2

subheader "Changing Host Directory Permissions..."
chown -R $1:$1 /home/$USER/http/hosts/$2
chmod 770 /home/$USER/http/hosts/$2

subheader "Adding Configuration..."
cp $MODULEPATH/$MODULE/etc/nginx/sites-available/template.conf /etc/nginx/sites-available/$1-$2.conf
string_replace_file /etc/nginx/sites-available/$1-$2.conf "\$USER" "$1"
string_replace_file /etc/nginx/sites-available/$1-$2.conf "\$HOST" "$2"
touch /etc/nginx/custom.d/$1-$2.conf
}

# Remove User
manage-host-manage-remove() {
subheader "Removing Host Configuration..."
rm -rf /etc/nginx/custom.d/$1-$2.conf
rm -rf /etc/nginx/sites-*/$1-$2.conf

subheader "Removing Host Directory..."
rm -rf /home/$1/http/hosts/$2
}

# Enable Host
manage-host-enable-host() {
subheader "Enabling Host..."
ln -s /etc/nginx/sites-available/$1-$2.conf /etc/nginx/sites-enabled/$1-$2.conf
}

# Disable Host
manage-host-disable-host() {
subheader "Disabling Host..."
rm /etc/nginx/sites-enabled/$1-$2.conf
}

# Enable Caching
manage-host-enable-cache() {
subheader "Enabling Caching..."
string_replace_file /etc/nginx/sites-available/$1-$2.conf "#include /etc/nginx/conf.d/cache.conf" "include /etc/nginx/conf.d/cache.conf"
}

# Disable Caching
manage-host-disable-cache() {
subheader "Disabling Caching..."
string_replace_file /etc/nginx/sites-available/$1-$2.conf "include /etc/nginx/conf.d/cache.conf" "#include /etc/nginx/conf.d/cache.conf"
}

# Enable Hidden File Access Denial
manage-host-enable-deny() {
subheader "Enabling Hidden File Block..."
string_replace_file /etc/nginx/sites-available/$1-$2.conf "#include /etc/nginx/conf.d/deny.conf" "include /etc/nginx/conf.d/deny.conf"
}

# Disable Hidden File Access Denial
manage-host-disable-deny() {
subheader "Disabling Hidden File Block..."
string_replace_file /etc/nginx/sites-available/$1-$2.conf "include /etc/nginx/conf.d/deny.conf" "#include /etc/nginx/conf.d/deny.conf"
}

# Enable PHP
manage-host-enable-php() {
subheader "Enabling PHP..."
string_replace_file /etc/nginx/sites-available/$1-$2.conf "#include /etc/nginx/php.d/" "include /etc/nginx/php.d/"
}

# Disable PHP
manage-host-disable-php() {
subheader "Disabling PHP..."
string_replace_file /etc/nginx/sites-available/$1-$2.conf "include /etc/nginx/php.d/" "#include /etc/nginx/php.d/"
}

# Enable SSL
manage-host-enable-ssl() {
subheader "Enabling SSL..."
string_replace_file /etc/nginx/sites-available/$1-$2.conf "#listen 443 ssl" "listen 443 ssl"
}

# Disable SSL
manage-host-disable-ssl() {
subheader "Disabling SSL..."
string_replace_file /etc/nginx/sites-available/$1-$2.conf "listen 443 ssl" "#listen 443 ssl"
}
}
31 changes: 26 additions & 5 deletions libraries/module/manage-user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ manage-user() {
read -p "Please enter a user: " USER

# Check Input
if grep -q '^[-0-9a-zA-Z]*$' <<< $1 || [[ $1 == "default" || $1 == "system" || $1 == "www-data" ]]; then
if grep -q '^[a-z][-a-z0-9_]*$' <<< $USER && [[ $USER != "default" || $USER != "system" || $USER != "www-data" ]]; then
# Exit Loop
break
else
Expand Down Expand Up @@ -87,11 +87,9 @@ manage-user() {
subheader "Removing User Home..."
rm -rf /home/$1

subheader "Removing User Database..."
#PLACEHOLDER#

subheader "Removing User HTTP..."
rm -rf /etc/nginx/php.d/$1.conf
rm -rf /etc/nginx/custom.d/$1-*.conf
rm -rf /etc/nginx/sites-*/$1-*.conf
rm -rf /etc/php5/fpm/pool.d/$1.conf
}
Expand Down Expand Up @@ -127,6 +125,25 @@ manage-user() {
subheader "Removing User from Group..."
deluser $1 $2
}

# Enable PHP for User
manage-user-enable-php() {
subheader "Enabling PHP for User..."
cp $MODULEPATH/manage-user-add/etc/php5/fpm/pool.d/template.conf /etc/php5/fpm/pool.d/$1.conf
string_replace_file /etc/php5/fpm/pool.d/$1.conf "\$USER" "$1"

subheader "Restarting Daemon..."
daemon_manage php5-fpm restart
}

# Disable PHP for User
manage-user-disable-php() {
subheader "Disabling PHP for User..."
rm /etc/php5/fpm/pool.d/$1.conf

subheader "Restarting Daemon..."
daemon_manage php5-fpm restart
}

####################
## Misc Functions ##
Expand All @@ -143,13 +160,17 @@ manage-user() {
# HTTP Directory
manage-user-http-directory() {
subheader "Creating HTTP Directory..."
mkdir -p /home/$1/http/{common,host,logs,secure}
mkdir -p /home/$1/http/{common,hosts,logs,secure}

subheader "Changing HTTP Directory Permissions..."
chown -R $1:$1 /home/$USER/http
find /home/$1/http -type d -exec chmod 770 {} \;

subheader "Adding User To WWW Group..."
gpasswd -a www-data $1

subheader "Adding PHP Configuration..."
cp $MODULEPATH/manage-user-add/etc/nginx/php.d/template.conf /etc/nginx/php.d/$1.conf
string_replace_file /etc/nginx/php.d/$1.conf "\$USER" "$1"
}
}
6 changes: 3 additions & 3 deletions modules/clean-packages/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Module Warning
warning "This module will remove all non-essential packages on this system, you have been warned!"
if ! (question --default yes "Do you still want to run this module and purge all non-essential packages? (Y/n)" || [ $UNATTENDED = 1 ]); then
if ! (question --default yes "Do you still want to run this module and purge all non-essential packages? (Y/n)" || [[ $UNATTENDED = 1 ]]); then
# Skipped Message
subheader "Skipping Module..."

Expand All @@ -20,13 +20,13 @@ subheader "Creating Package List..."
cp $MODULEPATH/$MODULE/$DISTRIBUTION-$VERSION/base-$ARCHITECTURE temp.list

# Check Platform
if [ $PLATFORM = "hardware" ]; then
if [[ $PLATFORM = "hardware" ]]; then
# Append Hardware Package List
cat $MODULEPATH/$MODULE/$DISTRIBUTION-$VERSION/base-hardware-$ARCHITECTURE >> temp.list
fi

# Check Platform Package List
if [ -f $MODULEPATH/$MODULE/$DISTRIBUTION-$VERSION/specific-$PLATFORM-$ARCHITECTURE ]; then
if [[ -f $MODULEPATH/$MODULE/$DISTRIBUTION-$VERSION/specific-$PLATFORM-$ARCHITECTURE ]]; then
# Append Platform Package List
cat $MODULEPATH/$MODULE/$DISTRIBUTION-$VERSION/specific-$PLATFORM-$ARCHITECTURE >> temp.list
fi
Expand Down
26 changes: 13 additions & 13 deletions modules/configure-general-system/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Configure (General): System Configuration

# Enable BASH History
if question --default no "Do you want to enable BASH history? (y/N)" || [ $(read_variable_module bash_history) = 1 ]; then
if question --default no "Do you want to enable BASH history? (y/N)" || [[ $(read_variable_module bash_history) = 1 ]]; then
subheader "Enabling BASH History..."
rm /etc/profile.d/disable_history.sh &> /dev/null
# Disable BASH History
Expand All @@ -12,39 +12,39 @@ else
fi

# Enable Additional Getty Instances
if question --default no "Do you want to enable extra getty instances (uneeded on virtual machines, can save memory if disabled)? (y/N)" || [ $(read_variable_module getty_extra) = 1 ]; then
if question --default no "Do you want to enable extra getty instances (uneeded on virtual machines, can save memory if disabled)? (y/N)" || [[ $(read_variable_module getty_extra) = 1 ]]; then
subheader "Enabling Additional Getty Instances..."
if [ $DISTRIBUTION = "debian" ]; then
if [[ $DISTRIBUTION = "debian" ]]; then
sed -e 's/^#\([2-6].*getty.*\)/\1/' -i /etc/inittab
elif [ $DISTRIBUTION = "ubuntu" ]; then
elif [[ $DISTRIBUTION = "ubuntu" ]]; then
rename.ul .conf.disabled .conf /etc/init/tty{3..6}.conf.disabled &> /dev/null
fi
# Disable Additional Getty Instances
else
subheader "Disabling Additional Getty Instances..."
if [ $DISTRIBUTION = "debian" ]; then
if [[ $DISTRIBUTION = "debian" ]]; then
sed -e "s/\(^[2-6].*getty.*\)/#\1/" -i /etc/inittab
elif [ $DISTRIBUTION = "ubuntu" ]; then
elif [[ $DISTRIBUTION = "ubuntu" ]]; then
rename.ul .conf .conf.disabled /etc/init/tty{3..6}.conf &> /dev/null
fi
fi

# Change Default System Shell
if question --default yes "Do you want to change the default system shell? (Y/n)" || [ $(read_variable_module shell) != 0 ]; then
if question --default yes "Do you want to change the default system shell? (Y/n)" || [[ $(read_variable_module shell) != 0 ]]; then
subheader "Changing Default System Shell..."
# Attended Mode
if [ $UNATTENDED = 0 ]; then
if [[ $UNATTENDED = 0 ]]; then
dpkg-reconfigure dash
# Unattended Mode
else
# Set BASH As Default
if [ $(read_variable_module shell) = "bash" ]; then
if [[ $(read_variable_module shell) = "bash" ]]; then
ln -fs bash /bin/sh
ln -fs dash /bin/sh.distrib
ln -fs bash.1.gz /usr/share/man/man1/sh.1.gz
ln -fs dash.1.gz /usr/share/man/man1/sh.distrib.1.gz
# Set DASH As Default
elif [ $(read_variable_module shell) = "dash" ]; then
elif [[ $(read_variable_module shell) = "dash" ]]; then
ln -fs dash /bin/sh
ln -fs bash /bin/sh.distrib
ln -fs dash.1.gz /usr/share/man/man1/sh.1.gz
Expand All @@ -57,16 +57,16 @@ if question --default yes "Do you want to change the default system shell? (Y/n)
fi

# Change System Timezone
if question --default yes "Do you want to change the system timezone? (Y/n)" || [ $(read_variable_module timezone) != 0 ]; then
if question --default yes "Do you want to change the system timezone? (Y/n)" || [[ $(read_variable_module timezone) != 0 ]]; then
subheader "Changing System Timezone..."
# Attended Mode
if [ $UNATTENDED = 0 ]; then
if [[ $UNATTENDED = 0 ]]; then
# Set Timezone Manually
dpkg-reconfigure tzdata
# Unattended Mode
else
# Check Timezone Existance
if [ -f /usr/share/zoneinfo/$(read_variable_module timezone) ]; then
if [[ -f /usr/share/zoneinfo/$(read_variable_module timezone) ]]; then
# Set Timezone From File
cp /usr/share/zoneinfo/$(read_variable_module timezone) /etc/localtime
echo $(read_variable_module timezone) > /etc/timezone
Expand Down
4 changes: 2 additions & 2 deletions modules/configure-general-user/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Configure (General): User Files/Settings

# Clean & Update Default User Files
if question --default yes "Do you want to clean and update default user files (in /etc/skel)? (Y/n)" || [ $(read_variable_module clean_default_skel) = 1 ]; then
if question --default yes "Do you want to clean and update default user files (in /etc/skel)? (Y/n)" || [[ $(read_variable_module clean_default_skel) = 1 ]]; then
subheader "Cleaning Default User Files..."
# Remove Skel Files
rm -rf /etc/skel/.??* /etc/skel/* &> /dev/null
Expand All @@ -21,7 +21,7 @@ if question --default yes "Do you want to clean and update default user files (i
fi

# Clean & Wipe Root Crontab
if question --default yes "Do you want to clean and wipe the root crontab? (Y/n)" || [ $(read_variable_module clean_root_crontab) = 1 ]; then
if question --default yes "Do you want to clean and wipe the root crontab? (Y/n)" || [[ $(read_variable_module clean_root_crontab) = 1 ]]; then
subheader "Cleaning Root Crontab..."
echo -n "" > temp
crontab -u root temp
Expand Down