|
| 1 | +#!/usr/bin/env bash |
| 2 | +##################################################### |
| 3 | +# Source https://mailinabox.email/ https://github.com/mail-in-a-box/mailinabox |
| 4 | +# Updated by afiniel-tech for yiimpool use... |
| 5 | +##################################################### |
| 6 | + |
| 7 | +source /etc/functions.sh |
| 8 | +cd ~/yiimpool/install |
| 9 | +clear |
| 10 | + |
| 11 | +# Welcome |
| 12 | +message_box "Yiimpool Installer v1.0" \ |
| 13 | +"Hello and thanks for using the Yiimpool Installer v1.0! |
| 14 | +\n\nInstallation for the most part is fully automated. In most cases any user responses that are needed are asked prior to the installation. |
| 15 | +\n\nNOTE: You should only install this on a brand new Ubuntu 16.04 or Ubuntu 18.04 installation." |
| 16 | +# Root warning message box |
| 17 | +message_box "Yiimpool Installer v1.0" \ |
| 18 | +"WARNING: You are trying to install as the root user! |
| 19 | +\n\nRunning any program as root is not recommended and can pose serious security risks that you want to avoid. |
| 20 | +\n\nThe next step you will be asked to create a new user account, you can name it whatever you want." |
| 21 | + |
| 22 | +# Ask if SSH key or password user |
| 23 | +dialog --title "Create New User With SSH Key" \ |
| 24 | +--yesno "Do you want to create your new user with SSH key login? |
| 25 | +Selecting no will create user with password login only." 7 60 |
| 26 | +response=$? |
| 27 | +case $response in |
| 28 | + 0) UsingSSH=yes;; |
| 29 | + 1) UsingSSH=no;; |
| 30 | + 255) echo "[ESC] key pressed.";; |
| 31 | +esac |
| 32 | + |
| 33 | +# If Using SSH Key Login |
| 34 | +if [[ ("$UsingSSH" == "yes") ]]; then |
| 35 | + clear |
| 36 | + if [ -z "${yiimpadmin:-}" ]; then |
| 37 | + DEFAULT_yiimpadmin=yiimpadmin |
| 38 | + input_box "New username" \ |
| 39 | + "Please enter your new username. |
| 40 | + \n\nUser Name:" \ |
| 41 | + ${DEFAULT_yiimpadmin} \ |
| 42 | + yiimpadmin |
| 43 | + |
| 44 | + if [ -z "${yiimpadmin}" ]; then |
| 45 | + # user hit ESC/cancel |
| 46 | + exit |
| 47 | + fi |
| 48 | + fi |
| 49 | + |
| 50 | + if [ -z "${ssh_key:-}" ]; then |
| 51 | + DEFAULT_ssh_key=PublicKey |
| 52 | + input_box "Please open PuTTY Key Generator on your local machine and generate a new public key." \ |
| 53 | + "To paste your Public key use ctrl shift right click. |
| 54 | + \n\nPublic Key:" \ |
| 55 | + ${DEFAULT_ssh_key} \ |
| 56 | + ssh_key |
| 57 | + |
| 58 | + if [ -z "${ssh_key}" ]; then |
| 59 | + # user hit ESC/cancel |
| 60 | + exit |
| 61 | + fi |
| 62 | + fi |
| 63 | + |
| 64 | + # create random user password |
| 65 | + RootPassword=$(openssl rand -base64 8 | tr -d "=+/") |
| 66 | + clear |
| 67 | + |
| 68 | + # Add user |
| 69 | + echo -e "Adding new user and setting SSH key...$COL_RESET" |
| 70 | + sudo adduser ${yiimpadmin} --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password |
| 71 | + echo -e "${RootPassword}\n${RootPassword}" | passwd ${yiimpadmin} |
| 72 | + sudo usermod -aG sudo ${yiimpadmin} |
| 73 | + # Create SSH Key structure |
| 74 | + mkdir -p /home/${yiimpadmin}/.ssh |
| 75 | + touch /home/${yiimpadmin}/.ssh/authorized_keys |
| 76 | + chown -R ${yiimpadmin}:${yiimpadmin} /home/${yiimpadmin}/.ssh |
| 77 | + chmod 700 /home/${yiimpadmin}/.ssh |
| 78 | + chmod 644 /home/${yiimpadmin}/.ssh/authorized_keys |
| 79 | + authkeys=/home/${yiimpadmin}/.ssh/authorized_keys |
| 80 | + echo "$ssh_key" > "$authkeys" |
| 81 | + |
| 82 | + # enabling yiimpool command |
| 83 | + echo '# yiimp |
| 84 | + # It needs passwordless sudo functionality. |
| 85 | + '""''"${yiimpadmin}"''""' ALL=(ALL) NOPASSWD:ALL |
| 86 | + ' | sudo -E tee /etc/sudoers.d/${yiimpadmin} >/dev/null 2>&1 |
| 87 | + |
| 88 | + echo ' |
| 89 | + cd ~/yiimpool/install |
| 90 | + bash start.sh |
| 91 | + ' | sudo -E tee /usr/bin/yiimpool >/dev/null 2>&1 |
| 92 | + sudo chmod +x /usr/bin/yiimpool |
| 93 | + |
| 94 | + # Check required files and set global variables |
| 95 | + cd $HOME/yiimpool/install |
| 96 | + source pre_setup.sh |
| 97 | + |
| 98 | + # Create the STORAGE_USER and STORAGE_ROOT directory if they don't already exist. |
| 99 | + if ! id -u $STORAGE_USER >/dev/null 2>&1; then |
| 100 | + sudo useradd -m $STORAGE_USER |
| 101 | + fi |
| 102 | + if [ ! -d $STORAGE_ROOT ]; then |
| 103 | + sudo mkdir -p $STORAGE_ROOT |
| 104 | + fi |
| 105 | + |
| 106 | + # Save the global options in /etc/yiimpool.conf so that standalone |
| 107 | + # tools know where to look for data. |
| 108 | + echo 'STORAGE_USER='"${STORAGE_USER}"' |
| 109 | + STORAGE_ROOT='"${STORAGE_ROOT}"' |
| 110 | + PUBLIC_IP='"${PUBLIC_IP}"' |
| 111 | + PUBLIC_IPV6='"${PUBLIC_IPV6}"' |
| 112 | + DISTRO='"${DISTRO}"' |
| 113 | + PRIVATE_IP='"${PRIVATE_IP}"'' | sudo -E tee /etc/yiimpool.conf >/dev/null 2>&1 |
| 114 | + |
| 115 | + sudo cp -r ~/yiimpool /home/${yiimpadmin}/ |
| 116 | + cd ~ |
| 117 | + sudo setfacl -m u:${yiimpadmin}:rwx /home/${yiimpadmin}/yiimpool |
| 118 | + sudo rm -r $HOME/yiimpool |
| 119 | + clear |
| 120 | + echo "New User is created and make sure you saved your private key..." |
| 121 | + echo -e "$RED Please reboot system and log in as $GREEN ${yiimpadmin} $COL_RESET $RED and type $COL_RESET $GREEN yiimpool $COL_RESET $RED to continue setup...$COL_RESET" |
| 122 | + exit 0 |
| 123 | +fi |
| 124 | + |
| 125 | +# New User Password Login Creation |
| 126 | +if [ -z "${yiimpadmin:-}" ]; then |
| 127 | + DEFAULT_yiimpadmin=yiimpadmin |
| 128 | + input_box "Creaete new username" \ |
| 129 | + "Please enter your new username. |
| 130 | + \n\nUser Name:" \ |
| 131 | + ${DEFAULT_yiimpadmin} \ |
| 132 | + yiimpadmin |
| 133 | + |
| 134 | + if [ -z "${yiimpadmin}" ]; then |
| 135 | + # user hit ESC/cancel |
| 136 | + exit |
| 137 | + fi |
| 138 | +fi |
| 139 | + |
| 140 | +if [ -z "${RootPassword:-}" ]; then |
| 141 | + DEFAULT_RootPassword=$(openssl rand -base64 8 | tr -d "=+/") |
| 142 | + input_box "User Password" \ |
| 143 | + "Enter your new user password or use this randomly system generated one. |
| 144 | + \n\nUnfortunatley dialog doesnt let you copy. So you have to write it down. |
| 145 | + \n\nUser password:" \ |
| 146 | + ${DEFAULT_RootPassword} \ |
| 147 | + RootPassword |
| 148 | + |
| 149 | + if [ -z "${RootPassword}" ]; then |
| 150 | + # user hit ESC/cancel |
| 151 | + exit |
| 152 | + fi |
| 153 | +fi |
| 154 | + |
| 155 | +clear |
| 156 | + |
| 157 | +dialog --title "Verify Your input" \ |
| 158 | +--yesno "Please verify your answers before you continue: |
| 159 | +
|
| 160 | +New User Name : ${yiimpadmin} |
| 161 | +New User Pass : ${RootPassword}" 8 60 |
| 162 | + |
| 163 | +# Get exit status |
| 164 | +# 0 means user hit [yes] button. |
| 165 | +# 1 means user hit [no] button. |
| 166 | +# 255 means user hit [Esc] key. |
| 167 | +response=$? |
| 168 | +case $response in |
| 169 | + |
| 170 | +0) |
| 171 | +clear |
| 172 | +echo -e " Adding new user and password...$COL_RESET" |
| 173 | + |
| 174 | +sudo adduser ${yiimpadmin} --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password |
| 175 | +echo -e ""${RootPassword}"\n"${RootPassword}"" | passwd ${yiimpadmin} |
| 176 | +sudo usermod -aG sudo ${yiimpadmin} |
| 177 | + |
| 178 | +# enabling yiimpool command |
| 179 | +echo '# yiimp |
| 180 | +# It needs passwordless sudo functionality. |
| 181 | +'""''"${yiimpadmin}"''""' ALL=(ALL) NOPASSWD:ALL |
| 182 | +' | sudo -E tee /etc/sudoers.d/${yiimpadmin} >/dev/null 2>&1 |
| 183 | + |
| 184 | +echo ' |
| 185 | +cd ~/yiimpool/install |
| 186 | +bash start.sh |
| 187 | +' | sudo -E tee /usr/bin/yiimpool >/dev/null 2>&1 |
| 188 | +sudo chmod +x /usr/bin/yiimpool |
| 189 | + |
| 190 | +# Check required files and set global variables |
| 191 | +cd $HOME/yiimpool/install |
| 192 | +source pre_setup.sh |
| 193 | + |
| 194 | +# Create the STORAGE_USER and STORAGE_ROOT directory if they don't already exist. |
| 195 | +if ! id -u $STORAGE_USER >/dev/null 2>&1; then |
| 196 | +sudo useradd -m $STORAGE_USER |
| 197 | +fi |
| 198 | +if [ ! -d $STORAGE_ROOT ]; then |
| 199 | +sudo mkdir -p $STORAGE_ROOT |
| 200 | +fi |
| 201 | + |
| 202 | +# Save the global options in /etc/yiimpool.conf so that standalone |
| 203 | +# tools know where to look for data. |
| 204 | +echo 'STORAGE_USER='"${STORAGE_USER}"' |
| 205 | +STORAGE_ROOT='"${STORAGE_ROOT}"' |
| 206 | +PUBLIC_IP='"${PUBLIC_IP}"' |
| 207 | +PUBLIC_IPV6='"${PUBLIC_IPV6}"' |
| 208 | +DISTRO='"${DISTRO}"' |
| 209 | +PRIVATE_IP='"${PRIVATE_IP}"'' | sudo -E tee /etc/yiimpool.conf >/dev/null 2>&1 |
| 210 | + |
| 211 | +sudo cp -r ~/yiimpool /home/${yiimpadmin}/ |
| 212 | +cd ~ |
| 213 | +sudo setfacl -m u:${yiimpadmin}:rwx /home/${yiimpadmin}/yiimpool |
| 214 | +sudo rm -r $HOME/yiimpool |
| 215 | +clear |
| 216 | +echo -e "$RED New User is $COL_RESET $GREEN Created $COL_RESET $RED...$COL_RESET" |
| 217 | +echo -e "$RED Please reboot system and log in as the new user $GREEN ${yiimpadmin} $COL_RESET and type$COL_RESET $GREEN yiimpool$COL_RESET $RED to continue setup...$COL_RESET" |
| 218 | +exit 0;; |
| 219 | + |
| 220 | +1) |
| 221 | + |
| 222 | +clear |
| 223 | +bash $(basename $0) && exit;; |
| 224 | + |
| 225 | +255) |
| 226 | + |
| 227 | +;; |
| 228 | +esac |
0 commit comments