#!/usr/bin/env bash # Author : Allan Christensen # First Created : 08042022 (DD-MM-YYYY) # Description : Installs Certbot with Cloudflare and Dns Challenge on Ubuntu 24.04 # License : MIT License (see LICENSE file for details) # # Are we root # if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo "" ; exit ; fi # # If snap is not installed then die # if ! command -v snap >/dev/null 2>&1; then echo "ERROR: snap is not installed aborting" ; exit 1 ; fi # # Function usage # usage () { printf -- "\ncertbotinstall\n\n" printf -- "Note: Must be run as root or using sudo\n\n" printf -- "Usage:\n" printf -- "sudo ./certbotinstall [-p] \n" printf -- " [-h] \n\n" printf -- "Examples:\n" printf -- "sudo ./certbotinstall -p 1234AkkbdceewEFJK\n\n" } # # Let's go # clear # # Check if no arguments were given # if [[ $# -eq 0 ]]; then usage; exit 1; fi # # Manual argument parsing # while [[ $# -gt 0 ]]; do case "$1" in -p) shift if [[ -z "$1" ]]; then usage printf "ERROR: API TOKEN CANNOT BE EMPTY!\n\n" exit 1 fi apitoken="$1" # # Validate Cloudflare API token format # if [[ "$apitoken" =~ [[:space:]] ]]; then printf "\nERROR: API token cannot contain spaces or line breaks.\n\n" exit 1 fi if [[ ! "$apitoken" =~ ^[A-Za-z0-9_-]{20,120}$ ]]; then printf "\nERROR: Invalid API token format.\n" printf "Expected 20-120 characters (A-Z, a-z, 0-9, - or _).\n\n" exit 1 fi shift ;; -h) usage exit 0 ;; *) usage printf "\nType: sudo %s -h for help\n\n" "$0" exit 1 ;; esac done # # Install Certbot # snap install core && snap refresh core snap install --classic certbot ln -sf /snap/bin/certbot /usr/bin/certbot # # Install Cloudflare plugin # snap set certbot trust-plugin-with-root=ok snap install certbot-dns-cloudflare # # Create the directory and the file that will store the api token # mkdir -p /etc/letsencrypt cat > /etc/letsencrypt/dnscloudflare.ini <