From 63c10f94ae59f7091c5ba157f097b00b505c8a4e Mon Sep 17 00:00:00 2001 From: Nathaniel Landau Date: Sun, 15 Mar 2015 15:16:10 -0400 Subject: [PATCH] Bumped to v2.0.0 - Moved to encrypted config files --- syncScripts/SyncTemplate.sh | 120 ++++++++++++++++++++++++++++-------- 1 file changed, 94 insertions(+), 26 deletions(-) diff --git a/syncScripts/SyncTemplate.sh b/syncScripts/SyncTemplate.sh index 7a14e02..d5088bc 100755 --- a/syncScripts/SyncTemplate.sh +++ b/syncScripts/SyncTemplate.sh @@ -4,7 +4,7 @@ # ################################################## # My Generic sync script. # -version="1.1.0" # Sets version variable +version="2.0.0" # Sets version variable # scriptTemplateVersion="1.1.1" # Version of scriptTemplate.sh that this script is based on # v.1.1.0 - Added 'debug' option @@ -40,6 +40,7 @@ scriptTemplateVersion="1.1.1" # Version of scriptTemplate.sh that this script is # * 2015-01-03 - v1.1.0 - Added support for using roots in Unison .prf # * 2015-03-10 - v1.1.1 - Updated script template version # - Removed $logFile from config. Default is now '~/library/logs/' +# * 2015-03-15 - v2.0.0 - Added support for encrypted config files. # # ################################################## @@ -80,6 +81,7 @@ verbose=0 force=0 strict=0 debug=0 +editConfig=0 # Set Temp Directory # ----------------------------------- @@ -102,18 +104,21 @@ tmpDir="/tmp/${scriptName}.$RANDOM.$RANDOM.$RANDOM.$$" logFile="$HOME/Library/Logs/${scriptBasename}.log" -# Configuration file +# Configuration file(s) # ----------------------------------- # This script calls for a configuration file. # This is its location. Default is the location # where it will be automatically created.` # ----------------------------------- -CONFIG="../etc/${scriptName}.cfg" +tmpConfig="${tmpDir}/${scriptName}.cfg" +newConfig="./${scriptName}.cfg" +encConfig="../etc/${scriptName}.cfg.enc" +############## Begin Script Functions Here ################### # Create new copy of the script if template is being executed function newCopy() { - if [ "${scriptName}" = "SyncTemplate.sh" ]; then + if [ "${scriptName}" = "syncTemplate.sh" ]; then input "name your new script:" read newname verbose "Copying SyncTemplate.sh to ${newname}" @@ -123,18 +128,56 @@ function newCopy() { fi } -function configFile() { +function encryptConfig() { +# If a non-encrypted config file exists (ie - it was being edited) we encrypt it + if is_file "${newConfig}"; then + verbose "${newConfig} exists" + seek_confirmation "Are you ready to encrypt your config file?" + if is_confirmed; then + if is_file "${encConfig}"; then + rm "${encConfig}" && verbose "Existing encoded config file exists. Running: rm ${encConfig}" + fi + if is_empty ${PASS}; then # Look for password from CLI + verbose "openssl enc -aes-256-cbc -salt -in ${newConfig} -out ${encConfig}" + openssl enc -aes-256-cbc -salt -in "${newConfig}" -out "${encConfig}" + else + verbose "openssl enc -aes-256-cbc -salt -in ${newConfig} -out ${encConfig} -k [PASSWORD]" + openssl enc -aes-256-cbc -salt -in "${newConfig}" -out "${encConfig}" -k ${PASS} + fi + rm "${newConfig}" && verbose "rm ${newConfig}" + success "Encoded the config file." + safeExit + else + warning "You need to encrypt your config file before proceeding" + safeExit + fi + fi +} + +function createTempConfig() { + # If we find the encoded config file, we decrypt it to the temp location + if is_file "${encConfig}"; then + if is_empty ${PASS}; then # Look for password from CLI + verbose "openssl enc -aes-256-cbc -d -in ${encConfig} -out ${tmpConfig}" + openssl enc -aes-256-cbc -d -in "${encConfig}" -out "${tmpConfig}" + else + verbose "openssl enc -aes-256-cbc -d -in ${encConfig} -out ${tmpConfig} -k [PASSWORD]" + openssl enc -aes-256-cbc -d -in "${encConfig}" -out "${tmpConfig}" -k ${PASS} + fi + fi +} + +function sourceConfiguration() { # Here we source the Config file or create a new one if none exists. - if is_file "${CONFIG}"; then - source "${CONFIG}" - verbose "source ${CONFIG}" + if is_file "${tmpConfig}"; then + source "${tmpConfig}" && verbose "source ${tmpConfig}" else seek_confirmation "Config file does not exist. Would you like to create one?" if is_not_confirmed; then - die "No config file. Exiting" + die "No config file." else - touch "${CONFIG}" && verbose "touch ${CONFIG}" - cat >"${CONFIG}" <"${newConfig}" <&2; safeExit ;; --version) echo "$(basename $0) $version"; safeExit ;; + -p|--password) shift; echo "Enter Pass: "; stty -echo; read PASS; stty echo; + echo ;; -v|--verbose) verbose=1 ;; -l|--log) printLog=1 ;; + -c|--config) editConfig=1 ;; -d|--debug) debug=1 ;; -q|--quiet) quiet=1 ;; -s|--strict) strict=1;; @@ -534,7 +599,10 @@ STARTTIME=$(date +"%s") header "${scriptName} Begun" newCopy -configFile +encryptConfig +createTempConfig +editConfiguration +sourceConfiguration hostCheck MethodCheck moutDrives @@ -550,4 +618,4 @@ TOTALTIME=$(($ENDTIME-$STARTTIME)) notifyPushover header "${scriptName} completed in $(convertsecs $TOTALTIME)" -safeExit # Exit cleanly \ No newline at end of file +safeExit # Exit cleanly