#!/bin/bash # # PluginInstaller helper for RaspAP # @author billz # license: GNU General Public License v3.0 # Exit on error set -o errexit readonly raspap_user="www-data" [ $# -lt 1 ] && { echo "Usage: $0 [parameters...]"; exit 1; } action="$1" # action to perform shift 1 case "$action" in "sudoers") [ $# -ne 1 ] && { echo "Usage: $0 sudoers "; exit 1; } file="$1" plugin_name=$(basename "$file") dest="/etc/sudoers.d/${plugin_name}" mv "$file" "$dest" || { echo "Error: Failed to move $file to $dest."; exit 1; } chown root:root "$dest" || { echo "Error: Failed to set ownership for $dest."; exit 1; } chmod 0440 "$dest" || { echo "Error: Failed to set permissions for $dest."; exit 1; } echo "OK" ;; "packages") [ $# -lt 1 ] && { echo "Usage: $0 packages "; exit 1; } echo "Installing APT packages..." for package in "$@"; do echo "Installing package: $package" apt-get install -y "$package" || { echo "Error: Failed to install $package."; exit 1; } done echo "OK" ;; "user") [ $# -lt 2 ] && { echo "Usage: $0 user ."; exit 1; } username=$1 password=$2 if id "$username" &>/dev/null; then # user already exists echo "OK" exit 0 fi # create the user without shell access useradd -r -s /bin/false "$username" # set password non-interactively echo "$username:$password" | chpasswd echo "OK" ;; "config") [ $# -lt 2 ] && { echo "Usage: $0 config "; exit 1; } source=$1 destination=$2 if [ ! -f "$source" ]; then echo "Source file $source does not exist." exit 1 fi mkdir -p "$(dirname "$destination")" cp "$source" "$destination" chown -R $raspap_user:$raspap_user "$destination" echo "OK" ;; "javascript") [ $# -lt 2 ] && { echo "Usage: $0 javascript "; exit 1; } source=$1 destination=$2 if [ ! -f "$source" ]; then echo "Source file $source does not exist." exit 1 fi if [ ! -d "$destination" ]; then mkdir -p "$destination" fi cp "$source" "$destination" chown -R $raspap_user:$raspap_user "$destination" echo "OK" ;; "plugin") [ $# -lt 2 ] && { echo "Usage: $0 plugin "; exit 1; } source=$1 destination=$2 if [ ! -d "$source" ]; then echo "Source directory $source does not exist." exit 1 fi plugin_dir=$(dirname "$destination") if [ ! -d "$plugin_dir" ]; then mkdir -p "$plugin_dir" fi cp -R "$source" "$destination" chown -R $raspap_user:$raspap_user "$plugin_dir" echo "OK" ;; "keys") [ $# -ne 4 ] && { echo "Usage: $0 keys "; exit 1; } key_url="$1" keyring="$2" repo="$3" list_file="$4" # add repository GPG key if it doesn't already exist if [ ! -f "$keyring" ]; then echo "Downloading GPG key from $key_url..." curl -fsSL "$key_url" | sudo tee "$keyring" > /dev/null || { echo "Error: Failed to download GPG key."; exit 1; } else echo "Repository GPG key already exists at $keyring" fi # add repository list if not present if [ ! -f "$list_file" ]; then echo "Adding repository $repo to sources list" curl -fsSL "$repo" | sudo tee "$list_file" > /dev/null || { echo "Error: Failed to add repository to sources list."; exit 1; } update_required=1 else echo "Repository already exists in sources list" fi # update apt package list if required if [ "$update_required" == "1" ]; then sudo apt-get update || { echo "Error: Failed to update apt"; exit 1; } fi echo "OK" ;; *) echo "Invalid action: $action" echo "Usage: $0 [parameters...]" echo "Actions:" echo " sudoers Install a sudoers file" echo " packages Install aptitude package(s)" echo " user Add user non-interactively" echo " config Applies a config file" echo " javascript Applies a JavaScript file" echo " plugin Copies a plugin directory" echo " keys Installs a GPG key for a third-party repo" exit 1 ;; esac