From 849106f7246e47bc077d7f3b8228cec717a8e63d Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Wed, 4 Jan 2023 16:54:15 +0000 Subject: [PATCH] feat(user/bk-gpg-ver-sigs):Add script to verify many gpg sig files - chore(unitproc/bktemp-yellDieTry):Rename bktemp-yellDieMust per monking's naming convention. --- .../{bktemp-yellDieTry => bktemp-yellDieMust} | 12 +- user/bk-gpg-ver-sigs | 206 ++++++++++++++++++ 2 files changed, 212 insertions(+), 6 deletions(-) rename unitproc/{bktemp-yellDieTry => bktemp-yellDieMust} (63%) create mode 100755 user/bk-gpg-ver-sigs diff --git a/unitproc/bktemp-yellDieTry b/unitproc/bktemp-yellDieMust similarity index 63% rename from unitproc/bktemp-yellDieTry rename to unitproc/bktemp-yellDieMust index fafa168..5d604ba 100644 --- a/unitproc/bktemp-yellDieTry +++ b/unitproc/bktemp-yellDieMust @@ -1,10 +1,10 @@ #!/bin/bash -# Desc: Defines bash functions yell(), die(), and try(), which are useful for +# Desc: Defines bash functions yell(), die(), and must(), which are useful for # indicating where in a script an error occurs. # Note: All three functions should be added together if used at all. # Ref/Attrib: [1] Yell, Die, Try Three-Fingered Claw technique https://stackoverflow.com/a/25515370 # Depends: GNU Coreutils 8.30 -# Version 0.1.1 +# Version 1.0.0 #==BEGIN Define script parameters== #==END Define script parameters== @@ -12,12 +12,12 @@ #===BEGIN Declare local script functions=== yell() { echo "$0: $*" >&2; } # print script path and all args to stderr die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status -try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails +must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails #===END Declare local script functions=== #==BEGIN sample code== yell "This message should appear in stderr."; -try echo "This message should appear in stdout."; -try eeeecho "This message should appear in an error message in stderr."; -yell "This message should not appear because \"try eeeecho\" failed."; +must echo "This message should appear in stdout."; +must eeeecho "This message should appear in an error message in stderr."; +yell "This message should not appear because \"must eeeecho\" failed."; #==END sample code== diff --git a/user/bk-gpg-ver-sigs b/user/bk-gpg-ver-sigs new file mode 100755 index 0000000..ab1bda8 --- /dev/null +++ b/user/bk-gpg-ver-sigs @@ -0,0 +1,206 @@ +#!/usr/bin/env bash +# Desc: Runs gpg --verify on all .asc, .gpg, and .sig files in specified directory. +# Version: 0.0.1 +# Depends: GNU parallel 20210822 +# Assumes: File names do not contain newlines. + +declare -Ag appRollCall # Associative array for storing app status +declare -Ag fileRollCall # Associative array for storing file status +declare -Ag dirRollCall # Associative array for storing dir status +find_pat_regex=".+\(sig\|asc\|gpg\)$"; +find_depth="10"; + +yell() { echo "$0: $*" >&2; } # print script path and all args to stderr +die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status +must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails +checkapp() { + # Desc: If arg is a command, save result in assoc array 'appRollCall' + # Usage: checkapp arg1 arg2 arg3 ... + # Version: 0.1.1 + # Input: global assoc. array 'appRollCall' + # Output: adds/updates key(value) to global assoc array 'appRollCall' + # Depends: bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command + appRollCall[$arg]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi; + else + appRollCall[$arg]="false"; returnState="false"; + fi; + done; + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi; +} # Check that app exists +checkfile() { + # Desc: If arg is a file path, save result in assoc array 'fileRollCall' + # Usage: checkfile arg1 arg2 arg3 ... + # Version: 0.1.2 + # Input: global assoc. array 'fileRollCall' + # Output: adds/updates key(value) to global assoc array 'fileRollCall'; + # Output: returns 0 if app found, 1 otherwise + # Depends: bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if [ -f "$arg" ]; then + fileRollCall["$arg"]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi; + elif [ -z "$arg" ]; then + fileRollCall["(no name)"]="false"; returnState="false"; + else + fileRollCall["$arg"]="false"; returnState="false"; + fi; + done; + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi; +} # Check that file exists +checkdir() { + # Desc: If arg is a dir path, save result in assoc array 'dirRollCall' + # Usage: checkdir arg1 arg2 arg3 ... + # Version 0.1.2 + # Input: global assoc. array 'dirRollCall' + # Output: adds/updates key(value) to global assoc array 'dirRollCall'; + # Output: returns 0 if all args are dirs; 1 otherwise + # Depends: Bash 5.0.3 + local returnState + + #===Process Args=== + for arg in "$@"; do + if [ -z "$arg" ]; then + dirRollCall["(Unspecified Dirname(s))"]="false"; returnState="false"; + elif [ -d "$arg" ]; then + dirRollCall["$arg"]="true"; + if ! [ "$returnState" = "false" ]; then returnState="true"; fi + else + dirRollCall["$arg"]="false"; returnState="false"; + fi + done + + #===Determine function return code=== + if [ "$returnState" = "true" ]; then + return 0; + else + return 1; + fi +} # Check that dir exists +displayMissing() { + # Desc: Displays missing apps, files, and dirs + # Usage: displayMissing + # Version 1.0.0 + # Input: associative arrays: appRollCall, fileRollCall, dirRollCall + # Output: stderr: messages indicating missing apps, file, or dirs + # Output: returns exit code 0 if nothing missing; 1 otherwise + # Depends: bash 5, checkAppFileDir() + local missingApps value appMissing missingFiles fileMissing + local missingDirs dirMissing + + #==BEGIN Display errors== + #===BEGIN Display Missing Apps=== + missingApps="Missing apps :"; + #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done + for key in "${!appRollCall[@]}"; do + value="${appRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing apps: $key => $value"; + missingApps="$missingApps""$key "; + appMissing="true"; + fi; + done; + if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing. + echo "$missingApps" 1>&2; + fi; + unset value; + #===END Display Missing Apps=== + + #===BEGIN Display Missing Files=== + missingFiles="Missing files:"; + #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done + for key in "${!fileRollCall[@]}"; do + value="${fileRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing files: $key => $value"; + missingFiles="$missingFiles""$key "; + fileMissing="true"; + fi; + done; + if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing. + echo "$missingFiles" 1>&2; + fi; + unset value; + #===END Display Missing Files=== + + #===BEGIN Display Missing Directories=== + missingDirs="Missing dirs:"; + #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done + for key in "${!dirRollCall[@]}"; do + value="${dirRollCall[$key]}"; + if [ "$value" = "false" ]; then + #echo "DEBUG:Missing dirs: $key => $value"; + missingDirs="$missingDirs""$key "; + dirMissing="true"; + fi; + done; + if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing. + echo "$missingDirs" 1>&2; + fi; + unset value; + #===END Display Missing Directories=== + + #==END Display errors== + #==BEGIN Determine function return code=== + if [ "$appMissing" == "true" ] || [ "$fileMissing" == "true" ] || [ "$dirMissing" == "true" ]; then + return 1; + else + return 0; + fi + #==END Determine function return code=== +} # Display missing apps, files, dirs +verify_file() { + # input: arg1: file path + path_in="$1" + + # Verify sig file + if gpg --verify "$path_in" 1>&2 2>/dev/random; then + printf "STATUS:Good sig for:%s\n" "$path_in"; + else + printf "STATUS:Bad sig for :%s\n" "$path_in"; + fi; +}; # verfiies one gpg file +main() { + # Check args + ## Note: Assume a single arg that is a dir + if [[ ! $# -eq 1 ]]; then die "FATAL:Incorrect number of args (must be 1):$#"; fi; + if [[ ! -d "$1" ]]; then die "FATAL:Not a dir:$1"; fi; + + # Check apps + if ! checkapp find gpg parallel; then displayMissing; printf "FATAL:Missing apps."; exit 1; fi; + + # Export bash functions (for GNU parallel) + export -f yell die must verify_file; + + # Generate file list + list_paths="$( find "$1" -maxdepth "$find_depth" -type f -iregex "$find_pat_regex" )"; + + # Verify files + echo "$list_paths" | parallel --group verify_file '{}'; +}; # main program + +main "$@"; + +exit 0; + +# find "$1" -maxdepth "$find_depth" -type f -iregex "find_pat_regex" | parallel if gpg --verify '{}' 2>/dev/random \; then echo "STATUS:Good sig:" '{}' \; else echo "STATUS:Bad sig :" '{}' \; fi\; -- 2.30.2