You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
5.9 KiB
204 lines
5.9 KiB
3 years ago
|
#!/usr/bin/env bash
|
||
2 years ago
|
if [ "$VERBOSE" == "yes" ]; then set -x; fi
|
||
2 years ago
|
YW=$(echo "\033[33m")
|
||
|
RD=$(echo "\033[01;31m")
|
||
|
BL=$(echo "\033[36m")
|
||
|
GN=$(echo "\033[1;92m")
|
||
|
CL=$(echo "\033[m")
|
||
3 years ago
|
RETRY_NUM=10
|
||
3 years ago
|
RETRY_EVERY=3
|
||
|
NUM=$RETRY_NUM
|
||
3 years ago
|
CM="${GN}✓${CL}"
|
||
|
CROSS="${RD}✗${CL}"
|
||
|
BFR="\\r\\033[K"
|
||
|
HOLD="-"
|
||
3 years ago
|
set -o errexit
|
||
|
set -o errtrace
|
||
|
set -o nounset
|
||
|
set -o pipefail
|
||
|
shopt -s expand_aliases
|
||
|
alias die='EXIT=$? LINE=$LINENO error_exit'
|
||
|
trap die ERR
|
||
|
|
||
|
function error_exit() {
|
||
|
trap - ERR
|
||
2 years ago
|
local reason="Unknown failure occurred."
|
||
3 years ago
|
local msg="${1:-$reason}"
|
||
|
local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
|
||
|
echo -e "$flag $msg" 1>&2
|
||
|
exit $EXIT
|
||
|
}
|
||
3 years ago
|
|
||
|
function msg_info() {
|
||
2 years ago
|
local msg="$1"
|
||
|
echo -ne " ${HOLD} ${YW}${msg}..."
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
function msg_ok() {
|
||
2 years ago
|
local msg="$1"
|
||
|
echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
function msg_error() {
|
||
2 years ago
|
local msg="$1"
|
||
|
echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
|
||
2 years ago
|
}
|
||
|
|
||
3 years ago
|
msg_info "Setting up Container OS "
|
||
3 years ago
|
sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
|
||
|
locale-gen >/dev/null
|
||
|
while [ "$(hostname -I)" = "" ]; do
|
||
2 years ago
|
echo 1>&2 -en "${CROSS}${RD} No Network! "
|
||
3 years ago
|
sleep $RETRY_EVERY
|
||
|
((NUM--))
|
||
2 years ago
|
if [ $NUM -eq 0 ]; then
|
||
|
echo 1>&2 -e "${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
|
||
3 years ago
|
exit 1
|
||
|
fi
|
||
|
done
|
||
2 years ago
|
msg_ok "Set up Container OS"
|
||
3 years ago
|
msg_ok "Network Connected: ${BL}$(hostname -I)"
|
||
3 years ago
|
|
||
2 years ago
|
set +e
|
||
|
alias die=''
|
||
2 years ago
|
if nc -zw1 8.8.8.8 443; then msg_ok "Internet Connected"; else
|
||
|
msg_error "Internet NOT Connected"
|
||
2 years ago
|
read -r -p "Would you like to continue anyway? <y/N> " prompt
|
||
|
if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
|
||
|
echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
|
||
|
else
|
||
|
echo -e " 🖧 Check Network Settings"
|
||
|
exit 1
|
||
|
fi
|
||
2 years ago
|
fi
|
||
2 years ago
|
RESOLVEDIP=$(nslookup "github.com" | awk -F':' '/^Address: / { matched = 1 } matched { print $2}' | xargs)
|
||
2 years ago
|
if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to $RESOLVEDIP"; fi
|
||
2 years ago
|
alias die='EXIT=$? LINE=$LINENO error_exit'
|
||
|
set -e
|
||
2 years ago
|
|
||
3 years ago
|
msg_info "Updating Container OS"
|
||
2 years ago
|
apt-get update &>/dev/null
|
||
|
apt-get -y upgrade &>/dev/null
|
||
3 years ago
|
msg_ok "Updated Container OS"
|
||
3 years ago
|
|
||
3 years ago
|
msg_info "Installing Dependencies"
|
||
3 years ago
|
apt-get update &>/dev/null
|
||
|
apt-get -qqy install \
|
||
2 years ago
|
git \
|
||
|
build-essential \
|
||
|
pkgconf \
|
||
|
libssl-dev \
|
||
|
libmariadb-dev-compat \
|
||
|
libpq-dev \
|
||
|
curl \
|
||
|
sudo &>/dev/null
|
||
3 years ago
|
msg_ok "Installed Dependencies"
|
||
3 years ago
|
|
||
2 years ago
|
WEBVAULT=$(curl -s https://api.github.com/repos/dani-garcia/bw_web_builds/releases/latest |
|
||
|
grep "tag_name" |
|
||
|
awk '{print substr($2, 2, length($2)-3) }')
|
||
3 years ago
|
|
||
2 years ago
|
VAULT=$(curl -s https://api.github.com/repos/dani-garcia/vaultwarden/releases/latest |
|
||
|
grep "tag_name" |
|
||
|
awk '{print substr($2, 2, length($2)-3) }')
|
||
3 years ago
|
|
||
3 years ago
|
msg_info "Installing Rust"
|
||
3 years ago
|
curl https://sh.rustup.rs -sSf | sh -s -- -y --profile minimal &>/dev/null
|
||
2 years ago
|
echo 'export PATH=~/.cargo/bin:$PATH' >>~/.bashrc &>/dev/null
|
||
3 years ago
|
export PATH=~/.cargo/bin:$PATH &>/dev/null
|
||
|
which rustc &>/dev/null
|
||
3 years ago
|
msg_ok "Installed Rust"
|
||
3 years ago
|
|
||
3 years ago
|
msg_info "Building Vaultwarden ${VAULT} (Patience)"
|
||
3 years ago
|
git clone https://github.com/dani-garcia/vaultwarden &>/dev/null
|
||
3 years ago
|
cd vaultwarden
|
||
|
cargo build --features "sqlite,mysql,postgresql" --release &>/dev/null
|
||
|
msg_ok "Built Vaultwarden ${VAULT}"
|
||
3 years ago
|
|
||
3 years ago
|
addgroup --system vaultwarden &>/dev/null
|
||
|
adduser --system --home /opt/vaultwarden --shell /usr/sbin/nologin --no-create-home --gecos 'vaultwarden' --ingroup vaultwarden --disabled-login --disabled-password vaultwarden &>/dev/null
|
||
|
mkdir -p /opt/vaultwarden/bin
|
||
|
mkdir -p /opt/vaultwarden/data
|
||
|
cp target/release/vaultwarden /opt/vaultwarden/bin/
|
||
3 years ago
|
|
||
3 years ago
|
msg_info "Downloading Web-Vault ${WEBVAULT}"
|
||
3 years ago
|
curl -fsSLO https://github.com/dani-garcia/bw_web_builds/releases/download/$WEBVAULT/bw_web_$WEBVAULT.tar.gz &>/dev/null
|
||
3 years ago
|
tar -xzf bw_web_$WEBVAULT.tar.gz -C /opt/vaultwarden/ &>/dev/null
|
||
|
msg_ok "Downloaded Web-Vault ${WEBVAULT}"
|
||
|
|
||
2 years ago
|
cat <<EOF >/opt/vaultwarden/.env
|
||
2 years ago
|
ADMIN_TOKEN=$(openssl rand -base64 48)
|
||
3 years ago
|
ROCKET_ADDRESS=0.0.0.0
|
||
|
DATA_FOLDER=/opt/vaultwarden/data
|
||
|
DATABASE_MAX_CONNS=10
|
||
|
WEB_VAULT_FOLDER=/opt/vaultwarden/web-vault
|
||
|
WEB_VAULT_ENABLED=true
|
||
|
EOF
|
||
|
|
||
|
msg_info "Creating Service"
|
||
|
chown -R vaultwarden:vaultwarden /opt/vaultwarden/
|
||
|
chown root:root /opt/vaultwarden/bin/vaultwarden
|
||
|
chmod +x /opt/vaultwarden/bin/vaultwarden
|
||
|
chown -R root:root /opt/vaultwarden/web-vault/
|
||
|
chmod +r /opt/vaultwarden/.env
|
||
3 years ago
|
service_path="/etc/systemd/system/vaultwarden.service" &>/dev/null
|
||
|
|
||
|
echo "[Unit]
|
||
|
Description=Bitwarden Server (Powered by Vaultwarden)
|
||
|
Documentation=https://github.com/dani-garcia/vaultwarden
|
||
|
After=network.target
|
||
|
[Service]
|
||
|
User=vaultwarden
|
||
|
Group=vaultwarden
|
||
3 years ago
|
EnvironmentFile=-/opt/vaultwarden/.env
|
||
|
ExecStart=/opt/vaultwarden/bin/vaultwarden
|
||
|
LimitNOFILE=65535
|
||
|
LimitNPROC=4096
|
||
3 years ago
|
PrivateTmp=true
|
||
|
PrivateDevices=true
|
||
|
ProtectHome=true
|
||
|
ProtectSystem=strict
|
||
3 years ago
|
DevicePolicy=closed
|
||
|
ProtectControlGroups=yes
|
||
|
ProtectKernelModules=yes
|
||
|
ProtectKernelTunables=yes
|
||
|
RestrictNamespaces=yes
|
||
|
RestrictRealtime=yes
|
||
|
MemoryDenyWriteExecute=yes
|
||
|
LockPersonality=yes
|
||
|
WorkingDirectory=/opt/vaultwarden
|
||
|
ReadWriteDirectories=/opt/vaultwarden/data
|
||
3 years ago
|
AmbientCapabilities=CAP_NET_BIND_SERVICE
|
||
|
[Install]
|
||
2 years ago
|
WantedBy=multi-user.target" >$service_path
|
||
3 years ago
|
systemctl daemon-reload
|
||
3 years ago
|
systemctl enable --now vaultwarden.service &>/dev/null
|
||
3 years ago
|
msg_ok "Created Service"
|
||
3 years ago
|
|
||
2 years ago
|
PASS=$(grep -w "root" /etc/shadow | cut -b6)
|
||
|
if [[ $PASS != $ ]]; then
|
||
|
msg_info "Customizing Container"
|
||
|
rm /etc/motd
|
||
|
rm /etc/update-motd.d/10-uname
|
||
|
touch ~/.hushlogin
|
||
|
GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
|
||
|
mkdir -p $(dirname $GETTY_OVERRIDE)
|
||
|
cat <<EOF >$GETTY_OVERRIDE
|
||
3 years ago
|
[Service]
|
||
|
ExecStart=
|
||
|
ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
|
||
|
EOF
|
||
2 years ago
|
systemctl daemon-reload
|
||
|
systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
|
||
|
msg_ok "Customized Container"
|
||
|
fi
|
||
2 years ago
|
if [[ "${SSH_ROOT}" == "yes" ]]; then
|
||
2 years ago
|
sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
|
||
|
systemctl restart sshd
|
||
2 years ago
|
fi
|
||
2 years ago
|
|
||
3 years ago
|
msg_info "Cleaning up"
|
||
3 years ago
|
apt-get autoremove >/dev/null
|
||
|
apt-get autoclean >/dev/null
|
||
3 years ago
|
msg_ok "Cleaned"
|