#!/bin/bash #################################################################################################### #### author: SlickStack ############################################################################ #### link: https://slickstack.io ################################################################### #### mirror: https://mirrors.slickstack.io/bash/ss-perms-nginx-ssl.txt ############################# #### path: /var/www/ss-perms-nginx-ssl ############################################################# #### destination: n/a (not a boilerplate) ########################################################## #### purpose: Resets all file and user permissions for SSL certs and related Nginx files ########### #### module version: Nginx 1.18.x, OpenSSL 3.0.x, Certbot 1.21.x ################################### #### sourced by: ss-encrypt-certbot, ss-encrypt-openssl, ss-install-nginx-ssl, ss-perms ############ #### bash aliases: ss perms letsencrypt, ss perms nginx ssl, ss perms openssl, ss perms ssl ######## #################################################################################################### ## source ss-config ## source /var/www/ss-config ## source ss-functions ## source /var/www/ss-functions ## BELOW THIS RELIES ON SS-CONFIG AND SS-FUNCTIONS #################################################################################################### #### TABLE OF CONTENTS (SS-Perms-Nginx-SSL) ######################################################## #################################################################################################### ## this is a brief summary of the different code snippets you will find in this script ## ## each section should be commented so you understand what is being accomplished ## ## A. Touch Timestamp File ## B. Message (Begin Script) ## C. Reset Permissions (SSL Certificates) #################################################################################################### #### A. SS-Perms-Nginx-SSL: Touch Timestamp File ################################################### #################################################################################################### ## this is a dummy timestamp file that will remember the last time this script was run ## ## it can be useful for developer reference and is sometimes used by SlickStack ## ss_touch "${TIMESTAMP_SS_PERMS_NGINX_SSL}" #################################################################################################### #### B. SS-Perms-Nginx-SSL: Message (Begin Script) ################################################# #################################################################################################### ## this is a simple message that announces to the shell the purpose of this bash script ## ## it will only be seen by sudo users who manually run this script in the shell ## ss_echo "${COLOR_INFO}Running ss-perms-nginx-ssl... ${COLOR_RESET}" #################################################################################################### #### C. SS-Perms-Nginx-SSL: Reset Permissions (SSL Certificates) ################################### #################################################################################################### ## although all permissions are reset in ss-perms we do it on per-module basis as well ## ## this ensures correct permissions in case this script is run individually etc ## ## create directories if not exist ## ss_mkdir /var/www/certs ss_mkdir /var/www/certs/keys ss_mkdir /var/www/html/.well-known ## certbot ss_mkdir /var/www/html/.well-known/acme-challenge ## certbot ## chown dirs ## ss_chown root:root /var/www/certs ## must be root:root ss_chown root:root /var/www/certs/keys ## must be root:root chown www-data:www-data /var/www/logs ## must be www-data:www-data chown www-data:www-data /var/www/meta ## must be www-data:www-data ss_chown "${SFTP_USER}":www-data /var/www/html/.well-known ## must be SFTP_USER:www-data ss_chown "${SFTP_USER}":www-data /var/www/html/.well-known/acme-challenge ## must be SFTP_USER:www-data ## chmod dirs ## chmod 0755 /var/www/certs ## must be 0755 chmod 0700 /var/www/certs/keys ## must be 0700 chmod 0775 /var/www/html/.well-known ## must be 0775 chmod 0775 /var/www/html/.well-known/acme-challenge ## must be 0775 ## chmod files ## find /var/www/certs -type f -exec chmod 0644 {} \; ## public certs/dhparam should be 0644 find /var/www/certs/keys -type f -exec chmod 0600 {} \; ## must be 0600 #################################################################################################### #### SlickStack: Reset Permissions (SlickStack Scripts) ############################################ #################################################################################################### ## we include this permissions reset in all cron jobs and bash scripts for redundancy ## ## chmod 0700 means only the root/sudo users can execute any SlickStack scripts ## ## THIS SNIPPET DOES NOT RELY ON SS-CONFIG OR SS-FUNCTIONS ## SNIPPET: ss bash scripts, ss cron jobs ## UPDATED: 02JUL2022 chown root:root /var/www/ss* ## must be root:root chown root:root /var/www/crons/*cron* ## must be root:root chown root:root /var/www/crons/custom/*cron* ## must be root:root chmod 0700 /var/www/ss* ## 0700 means only root/sudo can execute chmod 0700 /var/www/crons/*cron* ## 0700 means only root/sudo can execute chmod 0700 /var/www/crons/custom/*cron* ## 0700 means only root/sudo can execute #################################################################################################### #### SlickStack: External References Used To Improve This Script (Thanks, Interwebz) ############### #################################################################################################### ## Ref: https://unix.stackexchange.com/questions/87200/change-permissions-for-a-symbolic-link ## Ref: https://serverfault.com/questions/939682/lets-encrypt-certificate-permissions ## Ref: https://community.letsencrypt.org/t/recommended-permissions-on-files-distributed-by-lets-encrypt/104266 ## Ref: https://stackoverflow.com/questions/48868479/how-to-chmod-all-files-in-sub-directories-without-chmod-the-folders ## SS_EOF