├── README └── dazzle.sh /README: -------------------------------------------------------------------------------- 1 | # Dazzle, SparkleShare host setup script 2 | 3 | An easier and less error prone way to set up a SparkleShare host. 4 | Created to work on Debian and Red Hat based systems. 5 | 6 | 7 | ## Usage 8 | 9 | Usage (as root): 10 | 11 | # Get Dazzle 12 | curl https://raw.githubusercontent.com/hbons/Dazzle/master/dazzle.sh \ 13 | --output /usr/local/bin/dazzle 14 | 15 | # Initial Dazzle setup 16 | chmod +x /usr/local/bin/dazzle 17 | dazzle setup 18 | 19 | # Link a SparkleShare client 20 | dazzle link 21 | 22 | # Create a new project 23 | dazzle create PROJECT_NAME 24 | 25 | 26 | ## Configuration 27 | 28 | You can control almost all configuration options via environment variables: 29 | 30 | export DAZZLE_USER=dazzle 31 | export DAZZLE_HOME=/var/lib/stuff 32 | sudo dazzle setup 33 | 34 | Available options are the following: 35 | 36 | * DAZZLE_USER: the Dazzle user. Defaults to "storage". 37 | * DAZZLE_GROUP: the Dazzle group. Defaults to "storage". 38 | * DAZZLE_HOME: the directory used to store projects. Defaults to "/home/storage". 39 | -------------------------------------------------------------------------------- /dazzle.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # This program is free software. It comes without any warranty, to 4 | # the extent permitted by applicable law. You can redistribute it 5 | # and/or modify it under the terms of the Do What The Fuck You Want 6 | # To Public License, Version 2, as published by Sam Hocevar. See 7 | # http://sam.zoy.org/wtfpl/COPYING for more details. 8 | 9 | 10 | # Check if we're root, if not show a warning 11 | if [[ $UID -ne 0 ]]; then 12 | case $1 in 13 | ""|help) # You should be allowed to check the help without being root 14 | ;; 15 | *) 16 | echo "Sorry, but Dazzle needs to be run as root." 17 | exit 1 18 | ;; 19 | esac 20 | fi 21 | 22 | GIT=$( which git ) > /dev/null 23 | 24 | # Define text styles 25 | BOLD=$( tput bold ) 26 | NORMAL=$( tput sgr0 ) 27 | 28 | # Nice defaults 29 | DAZZLE_USER="${DAZZLE_USER:-storage}" 30 | DAZZLE_GROUP="${DAZZLE_GROUP:-$DAZZLE_USER}" 31 | DAZZLE_HOME="${DAZZLE_HOME:-/home/$DAZZLE_USER}" 32 | 33 | show_help () { 34 | echo "${BOLD}Dazzle, SparkleShare host setup script${NORMAL}" 35 | echo "This script needs to be run as root" 36 | echo 37 | echo "Usage: dazzle [COMMAND]" 38 | echo 39 | echo " setup configures this machine to serve as a SparkleShare host" 40 | echo " create PROJECT_NAME creates a SparkleShare project called PROJECT_NAME" 41 | echo " create-encrypted PROJECT_NAME creates an encrypted SparkleShare project" 42 | echo " link links a SparkleShare client to this host by entering a link code" 43 | echo 44 | } 45 | 46 | create_account () { 47 | STORAGE=$( grep "^$DAZZLE_USER:" /etc/passwd | cut -d : -f 1 ) 48 | 49 | # Create user 50 | if [ "$STORAGE" = "$DAZZLE_USER" ]; then 51 | echo " -> Account already exists." 52 | else 53 | STORAGE=$( grep "^$DAZZLE_GROUP:" /etc/group | cut -d : -f 1 ) 54 | GIT_SHELL=$( which git-shell ) 55 | 56 | if [ "$STORAGE" = "$DAZZLE_GROUP" ]; then 57 | echo " -> useradd $DAZZLE_USER --create-home --home $DAZZLE_HOME --system --shell $GIT_SHELL --password \"*\" --gid $DAZZLE_GROUP" 58 | useradd $DAZZLE_USER --create-home --home $DAZZLE_HOME --system --shell $GIT_SHELL --password "*" --gid $DAZZLE_GROUP 59 | 60 | else 61 | echo " -> useradd $DAZZLE_USER --create-home --home $DAZZLE_HOME --system --shell $GIT_SHELL --password \"*\" --user-group" 62 | useradd $DAZZLE_USER --create-home --home $DAZZLE_HOME --system --shell $GIT_SHELL --password "*" --user-group 63 | fi 64 | fi 65 | 66 | # Create base directory 67 | if [ ! -d "$DAZZLE_HOME" ]; then 68 | echo " -> mkdir --parents $DAZZLE_HOME" 69 | mkdir -p "$DAZZLE_HOME" 70 | fi 71 | 72 | sleep 0.5 73 | } 74 | 75 | configure_ssh () { 76 | echo " -> mkdir --parents $DAZZLE_HOME/.ssh" 77 | mkdir -p "$DAZZLE_HOME/.ssh" 78 | 79 | echo " -> touch $DAZZLE_HOME/.ssh/authorized_keys" 80 | touch "$DAZZLE_HOME/.ssh/authorized_keys" 81 | 82 | echo " -> chmod 700 $DAZZLE_HOME/.ssh" 83 | chmod 700 "$DAZZLE_HOME/.ssh" 84 | 85 | echo " -> chmod 600 $DAZZLE_HOME/.ssh/authorized_keys" 86 | chmod 600 "$DAZZLE_HOME/.ssh/authorized_keys" 87 | 88 | # Disable the password for the "storage" user to force authentication using a key 89 | CONFIG_CHECK=$( grep "^# SparkleShare$" /etc/ssh/sshd_config ) 90 | if ! [ "$CONFIG_CHECK" = "# SparkleShare" ]; then 91 | { 92 | echo "" 93 | echo "# SparkleShare" 94 | echo "# Please do not edit the above comment as it's used as a check by Dazzle" 95 | echo "Match User $DAZZLE_USER" 96 | echo " PasswordAuthentication no" 97 | echo " PubkeyAuthentication yes" 98 | echo "# End of SparkleShare configuration" 99 | } >> /etc/ssh/sshd_config 100 | fi 101 | 102 | sleep 0.5 103 | } 104 | 105 | reload_ssh_config () { 106 | if [ -f "/etc/init.d/sshd" ]; then 107 | echo " -> /etc/init.d/sshd reload" 108 | /etc/init.d/sshd reload >/dev/null 109 | 110 | elif [ -f "/etc/rc.d/sshd" ]; then 111 | echo " -> /etc/rc.d/sshd reload" 112 | /etc/rc.d/sshd reload >/dev/null 113 | 114 | elif [ -f "/etc/init.d/ssh" ]; then 115 | echo " -> /etc/init.d/ssh reload" 116 | /etc/init.d/ssh reload >/dev/null 117 | 118 | else 119 | echo " -> systemctl reload sshd" 120 | systemctl reload sshd 121 | fi 122 | } 123 | 124 | install_git_failed () { 125 | echo "${BOLD}Could not install Git... Please install it manually before continuing.${NORMAL}" 126 | echo 127 | exit 1 128 | } 129 | 130 | install_git () { 131 | if [ -n "$GIT" ]; then 132 | GIT_VERSION=$( $GIT --version | cut -b 13- ) 133 | echo " -> The Git package has already been installed (version $GIT_VERSION)." 134 | 135 | else 136 | if [ -f "/usr/bin/yum" ]; then 137 | echo " -> yum --assumeyes install git" 138 | if ! yum -y --quiet install git; then 139 | install_git_failed 140 | fi 141 | 142 | elif [ -f "/usr/bin/apt-get" ]; then 143 | echo " -> apt-get --yes install git" 144 | if apt-get --yes --quiet install git; then 145 | echo " -> apt-get --yes install git-core" 146 | if ! apt-get --yes --quiet install git-core; then 147 | install_git_failed 148 | fi 149 | else 150 | install_git_failed 151 | fi 152 | 153 | elif [ -f "/usr/bin/zypper" ]; then 154 | echo " -> zypper --yes install git-core" 155 | if ! zypper --yes --quiet install git-core; then 156 | install_git_failed 157 | fi 158 | 159 | elif [ -f "/usr/bin/emerge" ]; then 160 | echo " -> emerge dev-vcs/git" 161 | if ! emerge --quiet dev-vcs/git; then 162 | install_git_failed 163 | fi 164 | 165 | elif [ -f "/usr/bin/pacman" ]; then 166 | echo " -> pacman -S git" 167 | if ! pacman -S git; then 168 | install_git_failed 169 | fi 170 | 171 | else 172 | install_git_failed 173 | fi 174 | fi 175 | } 176 | 177 | create_project () { 178 | if [ -f "$DAZZLE_HOME/$1/HEAD" ]; then 179 | echo " -> Project \"$1\" already exists." 180 | echo 181 | else 182 | # Sanity check for Git 183 | if [ -z $GIT ]; then 184 | echo "Git is currently not installed! Run the \"dazzle setup\" command to fix this problem." 185 | exit 1 186 | fi 187 | 188 | # Create the Git repository 189 | echo " -> $GIT init --bare $DAZZLE_HOME/$1" 190 | $GIT init --quiet --bare "$DAZZLE_HOME/$1" 191 | 192 | # Don't allow force-pushing and data to get lost 193 | echo " -> $GIT config --file $DAZZLE_HOME/$1/config receive.denyNonFastForwards true" 194 | $GIT config --file "$DAZZLE_HOME/$1/config" receive.denyNonFastForwards true 195 | 196 | # Add list of files that Git should not compress 197 | EXTENSIONS="jpg jpeg png tiff gif psd xcf flac mp3 ogg oga avi mov mpg mpeg mkv ogv ogx webm zip gz bz xz bz2 rpm deb tgz rar ace 7z pak msi iso dmg" 198 | for EXTENSION in $EXTENSIONS; do 199 | sleep 0.05 200 | echo -ne " -> echo \"*.$EXTENSION -delta\" >> $DAZZLE_HOME/$1/info/attributes \r" 201 | echo "*.$EXTENSION -delta" >> "$DAZZLE_HOME/$1/info/attributes" 202 | sleep 0.05 203 | EXTENSION_UPPERCASE=$( echo "$EXTENSION" | tr '[:lower:]' '[:upper:]' ) 204 | echo -ne " -> echo \"*.$EXTENSION_UPPERCASE -delta\" >> $DAZZLE_HOME/$1/info/attributes \r" 205 | echo "*.$EXTENSION_UPPERCASE -delta" >> "$DAZZLE_HOME/$1/info/attributes" 206 | done 207 | 208 | echo "" 209 | 210 | # Set the right permissions 211 | echo " -> chown --recursive $DAZZLE_USER:$DAZZLE_GROUP $DAZZLE_HOME" 212 | chown -R "$DAZZLE_USER:$DAZZLE_GROUP" "$DAZZLE_HOME" 213 | 214 | echo " -> chmod --recursive o-rwx $DAZZLE_HOME/$1" 215 | chmod -R o-rwx "$DAZZLE_HOME"/"$1" 216 | 217 | sleep 0.5 218 | 219 | echo 220 | echo "${BOLD}Project \"$1\" was successfully created.${NORMAL}" 221 | fi 222 | 223 | # Fetch the external IP address 224 | # 1. fetch all inet addresses (IPv4) 225 | # 2. select only global scope addresses 226 | # 3. extract the address 227 | # 4. limit the list to the first address to get only one IP in case the server has more than one 228 | IP=$( ip -f inet addr |grep "inet .* scope global" | grep -Po "inet ([\d+\.]+)" | cut -c 6- | head -n1 ) 229 | PORT=$( grep --max-count=1 "^Port " /etc/ssh/sshd_config | cut -b 6- ) 230 | 231 | # Display info to link with the created project to the user 232 | echo "To link up a SparkleShare client, enter the following" 233 | echo "details into the ${BOLD}\"Add Hosted Project...\"${NORMAL} dialog: " 234 | echo 235 | echo " Address: ${BOLD}ssh://$DAZZLE_USER@$IP:$PORT${NORMAL}" 236 | echo " Remote Path: ${BOLD}$DAZZLE_HOME/$1${NORMAL}" 237 | echo 238 | echo "To link up (more) computers, use the \"dazzle link\" command." 239 | echo 240 | } 241 | 242 | link_client () { 243 | # Ask the user for the link code with a prompt 244 | echo "Paste your Client ID (found in the status icon menu) below and press ${BOLD}${NORMAL}." 245 | echo 246 | echo -n " ${BOLD}Client ID: ${NORMAL}" 247 | read -r LINK_CODE 248 | 249 | if [[ "$LINK_CODE" != ssh-rsa\ * ]]; then 250 | echo 251 | echo "${BOLD}The supplied string is not a valid Client ID!${NORMAL}" 252 | echo "Please try again." 253 | echo 254 | exit 1 255 | fi 256 | 257 | echo "$LINK_CODE" >> "$DAZZLE_HOME/.ssh/authorized_keys" 258 | echo 259 | echo "${BOLD}The client with this ID can now access projects.${NORMAL}" 260 | echo "Repeat this step to give access to more clients." 261 | echo 262 | } 263 | 264 | 265 | # Parse the command line arguments 266 | case $1 in 267 | setup) 268 | echo "${BOLD} 1/4 | Installing the Git package...${NORMAL}" 269 | install_git 270 | echo "${BOLD} 2/4 | Creating account \"$DAZZLE_USER\"...${NORMAL}" 271 | create_account 272 | echo "${BOLD} 3/4 | Configuring account \"$DAZZLE_USER\"...${NORMAL}" 273 | configure_ssh 274 | echo "${BOLD} 4/4 | Reloading the SSH config...${NORMAL}" 275 | reload_ssh_config 276 | echo 277 | echo "${BOLD}Setup complete!${NORMAL}" 278 | echo "To create a new project, run \"dazzle create PROJECT_NAME\"." 279 | echo 280 | ;; 281 | 282 | create) 283 | if [ -n "$2" ]; then 284 | echo "${BOLD}Creating project \"$2\"...${NORMAL}" 285 | create_project "$2" 286 | 287 | else 288 | echo "Please provide a project name." 289 | fi 290 | ;; 291 | 292 | create-encrypted) 293 | echo "${BOLD}Creating encrypted project \"$2\"...${NORMAL}" 294 | create_project "$2-crypto" 295 | ;; 296 | 297 | link) 298 | link_client "$2" 299 | ;; 300 | 301 | *|help) 302 | show_help 303 | ;; 304 | esac 305 | --------------------------------------------------------------------------------