2019-11-07 08:39:54 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# RaspAP SSL certificate installation functions
|
|
|
|
# author: @billz
|
|
|
|
# license: GNU General Public License v3.0
|
|
|
|
|
|
|
|
certname=$HOSTNAME."local"
|
|
|
|
lighttpd_ssl="/etc/lighttpd/ssl"
|
2019-11-07 12:33:17 +00:00
|
|
|
lighttpd_conf="/etc/lighttpd/lighttpd.conf"
|
2019-11-07 10:41:05 +00:00
|
|
|
webroot_dir="/var/www/html"
|
2019-11-07 08:39:54 +00:00
|
|
|
|
|
|
|
### NOTE: all the below functions are overloadable for system-specific installs
|
|
|
|
|
|
|
|
function config_installation() {
|
|
|
|
install_log "Configure a new SSL certificate"
|
2019-11-07 16:45:21 +00:00
|
|
|
echo "Current system hostname is $HOSTNAME"
|
2019-11-07 08:39:54 +00:00
|
|
|
echo -n "Create an SSL certificate for ${certname}? (Recommended) [y/N]"
|
|
|
|
if [ $assume_yes == 0 ]; then
|
|
|
|
read answer
|
|
|
|
if [[ $answer != "y" ]]; then
|
|
|
|
read -e -p "Enter an alternate certificate name: " -i "${certname}" certname
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo -e
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo -n "Install to Lighttpd SSL directory: ${lighttpd_ssl}? [y/N]: "
|
|
|
|
if [ $assume_yes == 0 ]; then
|
|
|
|
read answer
|
|
|
|
if [[ $answer != "y" ]]; then
|
2019-11-07 10:41:05 +00:00
|
|
|
read -e -p "Enter alternate Lighttpd SSL directory: " -i "${lighttpd_ssl}" lighttpd_ssl
|
2019-11-07 08:39:54 +00:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo -e
|
|
|
|
fi
|
2019-11-07 10:41:05 +00:00
|
|
|
|
|
|
|
install_divider
|
2019-11-07 08:39:54 +00:00
|
|
|
echo "A new SSL certificate for: ${certname}"
|
2019-11-07 10:41:05 +00:00
|
|
|
echo "will be installed to lighttpd SSL directory: ${lighttpd_ssl}"
|
|
|
|
install_divider
|
2019-11-07 08:39:54 +00:00
|
|
|
echo -n "Complete installation with these values? [y/N]: "
|
|
|
|
if [ $assume_yes == 0 ]; then
|
|
|
|
read answer
|
|
|
|
if [[ $answer != "y" ]]; then
|
|
|
|
echo "Installation aborted."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo -e
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Installs pre-built mkcert binary for Arch Linux ARM
|
|
|
|
function install_mkcert() {
|
|
|
|
install_log "Fetching mkcert binary"
|
2019-11-07 10:41:05 +00:00
|
|
|
sudo wget -q https://github.com/FiloSottile/mkcert/releases/download/v1.3.0/mkcert-v1.3.0-linux-arm -O /usr/local/bin/mkcert || install_error "Unable to download mkcert"
|
2019-11-07 08:39:54 +00:00
|
|
|
sudo chmod +x /usr/local/bin/mkcert
|
|
|
|
|
|
|
|
install_log "Installing mkcert"
|
|
|
|
mkcert -install || install_error "Failed to install mkcert"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Generate a certificate for host
|
|
|
|
function generate_certificate() {
|
|
|
|
install_log "Generating a new certificate for $certname"
|
|
|
|
cd /home/pi
|
|
|
|
mkcert $certname "*.${certname}.local" $certname || install_error "Failed to generate certificate for $certname"
|
|
|
|
|
|
|
|
install_log "Combining private key and certificate"
|
2019-11-07 10:41:05 +00:00
|
|
|
cat $certname+2-key.pem $certname+2.pem > $certname.pem || install_error "Failed to combine key and certificate"a
|
|
|
|
echo "OK"
|
2019-11-07 08:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Create a directory for the combined .pem file in lighttpd
|
|
|
|
function create_lighttpd_dir() {
|
|
|
|
install_log "Create SLL directory for lighttpd"
|
2019-11-07 12:33:17 +00:00
|
|
|
if [ ! -d "$lighttpd_ssl" ]; then
|
|
|
|
sudo mkdir -p "$lighttpd_ssl" || install_error "Failed to create lighttpd directory"
|
|
|
|
fi
|
2019-11-07 10:41:05 +00:00
|
|
|
echo "OK"
|
2019-11-07 08:39:54 +00:00
|
|
|
|
2019-11-07 10:41:05 +00:00
|
|
|
install_log "Setting permissions and moving .pem file"
|
2019-11-07 08:39:54 +00:00
|
|
|
chmod 400 /home/pi/"$certname".pem || install_error "Unable to set permissions for .pem file"
|
|
|
|
sudo mv /home/pi/"$certname".pem /etc/lighttpd/ssl
|
2019-11-07 10:41:05 +00:00
|
|
|
echo "OK"
|
2019-11-07 08:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Edit the lighttpd configuration
|
|
|
|
function configure_lighttpd() {
|
|
|
|
install_log "Configuring lighttpd for SSL"
|
2019-11-07 12:33:17 +00:00
|
|
|
# Generate config to enable SSL in lighttpd
|
|
|
|
lines=(
|
|
|
|
'server.modules += ("mod_openssl")'
|
|
|
|
'$SERVER["socket"] == ":443" {'
|
|
|
|
'ssl.engine = "enable"'
|
|
|
|
'ssl.pemfile = "'$lighttpd_ssl/$certname'.pem"'
|
|
|
|
'ssl.ca-file = "/home/pi/.local/share/mkcert/rootCA.pem"'
|
|
|
|
'server.name = "'$certname'"'
|
|
|
|
'server.document-root = "'${webroot_dir}'"}'
|
|
|
|
)
|
|
|
|
for line in "${lines[@]}"; do
|
|
|
|
if grep -Fxq "${line}" "${lighttpd_conf}" > /dev/null; then
|
|
|
|
echo "$line: Line already added"
|
|
|
|
else
|
|
|
|
sudo sed -i "$ a $line" $lighttpd_conf
|
|
|
|
echo "Adding line $line"
|
|
|
|
fi
|
|
|
|
#echo $line
|
|
|
|
done
|
2019-11-07 10:41:05 +00:00
|
|
|
echo "OK"
|
2019-11-07 08:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Copy rootCA.pem to RaspAP web root
|
|
|
|
function copy_rootca() {
|
2019-11-07 10:41:05 +00:00
|
|
|
install_log "Copying rootCA.pem to RaspAP web root" || install_error "Unable to copy rootCA.pem to ${webroot_dir}"
|
2019-11-07 08:39:54 +00:00
|
|
|
sudo cp /home/pi/.local/share/mkcert/rootCA.pem ${webroot_dir}
|
2019-11-07 10:41:05 +00:00
|
|
|
echo "OK"
|
2019-11-07 08:39:54 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 10:41:05 +00:00
|
|
|
# Restart lighttpd service
|
|
|
|
function restart_lighttpd() {
|
|
|
|
install_log "Restarting lighttpd service"
|
|
|
|
sudo systemctl restart lighttpd.service || install_error "Unable to restart lighttpd service"
|
|
|
|
sudo systemctl status lighttpd.service
|
|
|
|
}
|
2019-11-07 08:39:54 +00:00
|
|
|
|
2019-11-07 10:41:05 +00:00
|
|
|
function install_complete() {
|
|
|
|
install_log "SSL certificate install completed!"
|
|
|
|
install_divider
|
|
|
|
echo "Open a browser and enter the address: http://${certname}/rootCA.pem"
|
|
|
|
echo "Download the root certificate to your client and add it to your system keychain."
|
|
|
|
echo "Note: Be sure to set this certificate to "Always trust" to avoid browser warnings."
|
|
|
|
echo "Finally, enter the address https://${certname} in your browser."
|
|
|
|
echo "Enjoy an encrypted SSL connection to RaspAP 🔒"
|
2019-11-07 13:24:06 +00:00
|
|
|
echo "For advanced options, run mkcert -help"
|
2019-11-07 10:41:05 +00:00
|
|
|
install_divider
|
2019-11-07 08:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function install_certificate() {
|
|
|
|
display_welcome
|
|
|
|
config_installation
|
|
|
|
install_mkcert
|
|
|
|
generate_certificate
|
|
|
|
create_lighttpd_dir
|
|
|
|
configure_lighttpd
|
|
|
|
copy_rootca
|
|
|
|
restart_lighttpd
|
|
|
|
install_complete
|
|
|
|
}
|
|
|
|
|