X-Git-Url: https://zdv2.bktei.com/gitweb/EVA-2020-02.git/blobdiff_plain/21363b806b9f1e8270be701f7f1929be47ed690e..492d1a57f8b51ef18a177551091b451f92a86cba:/exec/bklog?ds=sidebyside diff --git a/exec/bklog b/exec/bklog index 6493015..38f4f3f 100644 --- a/exec/bklog +++ b/exec/bklog @@ -11,7 +11,7 @@ dirTmpDefault="/dev/shm"; # Default parent of working directory # Script Metadata scriptName="bklog"; # Define basename of script file. -scriptVersion="0.1.13"; # Define version of script. +scriptVersion="0.1.16"; # Define version of script. scriptURL="https://gitlab.com/baltakatei/ninfacyzga-01"; # Define wesite hosting this script. scriptTimeStart="$(date +%Y%m%dT%H%M%S.%N)"; # YYYYmmddTHHMMSS.NNNNNNNNN scriptHostname=$(hostname); # Save hostname of system running this script. @@ -23,7 +23,6 @@ ageURL="https://github.com/FiloSottile/age/releases/tag/v1.0.0-beta2"; # Define declare -a buffer # array for storing while read buffer declare -a argRecPubKeys # array for processArguments function declare -a recPubKeysValid # array for storing both '-r' and '-R' recipient pubkeys -declare -a recPubKeysValidStatic # for storing '-r' recipient pubkeys declare -a argProcStrings argProcFileExts # for storing buffer processing strings (ex: "gpsbabel -i nmea -f - -o gpx -F - ") declare -Ag appRollCall # Associative array for storing app status declare -Ag fileRollCall # Associative array for storing file status @@ -43,22 +42,22 @@ try() { "$@" || die "cannot $*"; } #o processArguments() { while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0). case "$1" in - -v | --verbose) optionVerbose="true"; vbm "DEBUG:Verbose mode enabled.";; # Enable verbose mode. + -v | --verbose) optionVerbose="true"; vbm "DEBUG :Verbose mode enabled.";; # Enable verbose mode. -h | --help) showUsage; exit 1;; # Display usage. --version) showVersion; exit 1;; # Show version - -o | --output) if [ -d "$2" ]; then dirOut="$2"; vbm "DEBUG:dirOut:$dirOut"; shift; fi ;; # Define output directory. - -e | --encrypt) optionEncrypt="true"; vbm "DEBUG:Encrypted output mode enabled.";; # Enable encryption - -r | --recipient) optionRecipients="true"; argRecPubKeys+=("$2"); vbm "STATUS:pubkey added:""$2"; shift;; # Add recipients - -c | --compress) optionCompress="true"; vbm "DEBUG:Compressed output mode enabled.";; # Enable compression + -o | --output) if [ -d "$2" ]; then dirOut="$2"; vbm "DEBUG :dirOut:$dirOut"; shift; fi ;; # Define output directory. + -e | --encrypt) optionEncrypt="true"; vbm "DEBUG :Encrypted output mode enabled.";; # Enable encryption + -r | --recipient) optionRecArg="true"; argRecPubKeys+=("$2"); vbm "STATUS:pubkey added:""$2"; shift;; # Add recipients + -R | --recipient-dir) optionRecDir="true" && argRecDir="$2"; shift;; # Add recipient watch dir + -c | --compress) optionCompress="true"; vbm "DEBUG :Compressed output mode enabled.";; # Enable compression -z | --time-zone) try setTimeZoneEV "$2"; shift;; # Set timestamp timezone -t | --temp-dir) optionTmpDir="true" && argTempDirPriority="$2"; shift;; # Set time zone - -R | --recipient-dir) optionRecipients="true"; optionRecDir="true" && argRecDir="$2"; shift;; # Add recipient watch dir -b | --buffer-ttl) optionCustomBufferTTL="true" && argCustomBufferTTL="$2"; shift;; # Set custom buffer period (default: 300 seconds) -B | --script-ttl) optionCustomScriptTTL_TE="true" && argCustomScriptTTL_TE="$2"; shift;; # Set custom script TTL (default: "day") -p | --process-string) optionProcString="true" && argProcStrings+=("$2") && argProcFileExts+=("$3") && vbm "STATUS:file extension \"$2\" for output of processing string added:\"$3\""; shift; shift;; - -l | --label) optionLabel="true" && argLabel="$2"; vbm "DEBUG:Custom label received:$argLabel"; shift;; - -w | --store-raw) optionStoreRaw="true" && argRawFileExt="$2"; vbm "DEBUG:Raw stdin file extension received:$argRawFileExt"; shift;; - -W | --no-store-raw) optionNoStoreRaw="true"; vbm "DEBUG:Option selected to not store raw stdin data."; shift;; + -l | --label) optionLabel="true" && argLabel="$2"; vbm "DEBUG :Custom label received:$argLabel"; shift;; + -w | --store-raw) optionStoreRaw="true" && argRawFileExt="$2"; vbm "DEBUG :Raw stdin file extension received:$argRawFileExt"; shift;; + -W | --no-store-raw) optionNoStoreRaw="true"; vbm "DEBUG :Option selected to not store raw stdin data."; shift;; *) yell "ERROR: Unrecognized argument: $1"; yell "STATUS:All arguments:$*"; exit 1;; # Handle unrecognized options. esac shift @@ -66,7 +65,7 @@ processArguments() { } # Argument Processing vbm() { # Description: Prints verbose message ("vbm") to stderr if optionVerbose is set to "true". - # Usage: vbm "DEBUG:verbose message here" + # Usage: vbm "DEBUG :verbose message here" # Version 0.1.2 # Input: arg1: string # vars: optionVerbose @@ -174,11 +173,11 @@ displayMissing() { #==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 echo "DEBUG :$key => ${appRollCall[$key]}"; done for key in "${!appRollCall[@]}"; do value="${appRollCall[$key]}"; if [ "$value" = "false" ]; then - #echo "DEBUG:Missing apps: $key => $value"; + #echo "DEBUG :Missing apps: $key => $value"; missingApps="$missingApps""$key "; appMissing="true"; fi; @@ -191,11 +190,11 @@ displayMissing() { #===BEGIN Display Missing Files=== missingFiles="Missing files:"; - #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done + #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"; + #echo "DEBUG :Missing files: $key => $value"; missingFiles="$missingFiles""$key "; fileMissing="true"; fi; @@ -208,11 +207,11 @@ displayMissing() { #===BEGIN Display Missing Directories=== missingDirs="Missing dirs:"; - #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done + #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"; + #echo "DEBUG :Missing dirs: $key => $value"; missingDirs="$missingDirs""$key "; dirMissing="true"; fi; @@ -245,7 +244,7 @@ appendFileTar(){ # Save function name fn="${FUNCNAME[0]}"; - #yell "DEBUG:STATUS:$fn:Started appendFileTar()." + #yell "DEBUG :STATUS:$fn:Started appendFileTar()." # Set file name if ! [ -z "$2" ]; then fileName="$2"; else yell "ERROR:$fn:Not enough arguments."; exit 1; fi @@ -264,7 +263,7 @@ appendFileTar(){ # Append to tar try tar --append --directory="$tmpDir" --file="$tarPath" "$fileName"; - #yell "DEBUG:STATUS:$fn:Finished appendFileTar()." + #yell "DEBUG :STATUS:$fn:Finished appendFileTar()." } # Append [processed] file to Tar archive checkAgePubkey() { # Desc: Checks if string is an age-compatible pubkey @@ -427,79 +426,6 @@ setTimeZoneEV(){ return 0; fi } # Exports TZ environment variable -showUsage() { - cat <<'EOF' - USAGE: - cmd | bklog [ options ] - - OPTIONS: - -h, --help - Display help information. - --version - Display script version. - -v, --verbose - Display debugging info. - -e, --encrypt - Encrypt output. - -r, --recipient [ string pubkey ] - Specify recipient. May be age or ssh pubkey. - May be specified multiple times for multiple pubkeys. - See https://github.com/FiloSottile/age - -o, --output [ path dir ] - Specify output directory to save logs. This option is required - to save log data. - -p, --process-string [ filter command ] [ output file extension] - Specify how to create and name a processed version of the stdin. - For example, if stdin is 'nmea' location data: - - -p "gpsbabel -i nmea -f - -o gpx -F - " ".gpx" - - This option would cause the stdin to 'bklog' to be piped into - the 'gpsbabel' command, interpreted as 'nmea' data, converted - into 'gpx' format, and then appended to the output tar file - as a file with a '.gpx' extension. - This option may be specified multiple times in order to output - results of multiple different processing methods. - -l, --label [ string ] - Specify a label to be included in all output file names. - Ex: 'location' if stdin is location data. - -w, --store-raw [ file extension ] - Specify file extension of file within output tar that contains - raw stdin data. The default behavior is to always save raw stdin - data in a '.stdin' file. Example usage when 'bklog' receives - 'nmea' data from 'gpspipe -r': - - -w ".nmea" - - Stdin data is saved in a '.nmea' file within the output tar. - -W, --no-store-raw - Do not store raw stdin in output tar. - -c, --compress - Compress output with gzip (before encryption if enabled). - -z, --time-zone - Specify time zone. (ex: "America/New_York") - -t, --temp-dir [path dir] - Specify parent directory for temporary working directory. - Default: "/dev/shm" - -R, --recipient-dir [path dir] - Specify directory containing files whose first lines are - to be interpreted as pubkey strings (see '-r' option). - -b, --buffer-ttl [integer] - Specify custom buffer period in seconds (default: 300 seconds) - -B, --script-ttl [time element string] - Specify custom script time-to-live in seconds (default: "day") - Valid values: "day", "hour" - - EXAMPLE: (bash script lines) - $ gpspipe -r | /bin/bash bklog -v -e -c -z "UTC" -t "/dev/shm" \ - -r age1mrmfnwhtlprn4jquex0ukmwcm7y2nxlphuzgsgv8ew2k9mewy3rs8u7su5 \ - -r age1ala848kqrvxc88rzaauc6vc5v0fqrvef9dxyk79m0vjea3hagclswu0lgq \ - -R ~/.config/bklog/recipients -w ".nmea" -b 300 -B "day" \ - -o ~/Sync/Logs -l "location" \ - -p "gpsbabel -i nmea -f - -o gpx -F - " ".gpx" \ - -p "gpsbabel -i nmea -f - -o kml -F - " ".kml" -EOF -} # Display information on how to use this script. showVersion() { yell "$scriptVersion" } # Display script version. @@ -852,7 +778,7 @@ magicInitWorkingDir() { fi; fi; ## Set dir_tmp using dir_tmp_parent and nonce (scriptTimeStart) - dir_tmp="$dir_tmp_parent"/"$scriptTimeStart""..bkgpslog" && vbm "DEBUG:$fn:Set dir_tmp to:$dir_tmp"; # Note: removed at end of main(). + dir_tmp="$dir_tmp_parent"/"$scriptTimeStart""..bkgpslog" && vbm "DEBUG :$fn:Set dir_tmp to:$dir_tmp"; # Note: removed at end of main(). vbm "STATUS:$fn:Finished magicInitWorkingDir() function."; } # Sets working dir magicInitCheckTar() { @@ -898,7 +824,7 @@ magicParseCompressionArg() { else cmd_compress="tee /dev/null " && vbm "STATUS:$fn:cmd_compress:$cmd_compress"; cmd_compress_suffix="" && vbm "STATUS:$fn:cmd_compress_suffix:$cmd_compress_suffix"; - vbm "DEBUG:$fn:Compression not enabled."; + vbm "DEBUG :$fn:Compression not enabled."; fi; vbm "STATUS:$fn:Starting magicParseCompressionArg() function."; } # Form compression cmd string and filename suffix @@ -988,28 +914,40 @@ magicParseProcessStrings() { # Validate input ## Validate argRawFileExt if [[ "$argRawFileExt" =~ ^[.][[:alnum:]]*$ ]]; then - rawFileExt="$argRawFileExt"; + rawFileExt="$argRawFileExt" && \ + vbm "DEBUG :$fn:Set rawFileExt to \"$argRawFileExt\""; + else + vbm "DEBUG :$fn:Validation failure for $argRawFileExt . Not set to rawFileExt."; fi; # Add default stdin output file entries for procStrings, procFileExts ## Check if user specified that no raw stdin be saved. if [[ ! "$optionNoStoreRaw" = "true" ]]; then ### T: --no-store-raw not set. Store raw. Append procStrings with cat. + vbm "DEBUG :$fn:--no-store-raw not set. Storing raw."; #### Append procStrings array - procStrings+=("cat "); + procStrings+=("cat ") && \ + vbm "DEBUG :$fn:Appended \"cat \" to procStrings"; + vbm "DEBUG :$fn:procStrings array:${procStrings[*]}"; #### Check if --store-raw set. if [[ "$optionStoreRaw" = "true" ]]; then ##### T: --store-raw set. Append procFileExts with user-specified file ext - procFileExts+=("$rawFileExt"); + vbm "DEBUG :$fn:--store-raw set."; + procFileExts+=("$rawFileExt") && \ + vbm "DEBUG :$fn:Appended $rawFileExt to procFileExts"; + vbm "STATUS:$fn:procFileExts array:${procFileExts[*]}"; else ##### F: --store-raw not set. Append procFileExts with default ".stdin" file ext ###### Append procFileExts array - procFileExts+=(".stdin"); + procFileExts+=(".stdin") && \ + vbm "DEBUG :$fn:Appended \".stdin\" to procFileExts"; + vbm "STATUS:$fn:procFileExts array:${procFileExts[*]}"; fi; else ### F: --no-store-raw set. Do not store raw. #### Do not append procStrings or procFileExts arrays. - : + vbm "STATUS:$fn:--no-store-raw set. Not storing raw."; + vbm "STATUS:$fn:procFileExts array:${procFileExts[*]}"; fi; # Do nothing more if optionProcString not set to true. @@ -1032,113 +970,108 @@ magicParseProcessStrings() { yell "ERROR:$fn:Illegal character '-' at start of process string element:\"$element\""; exit 1; fi; done; vbm "STATUS:$fn:Quick check shows argProcStrings and argProcFileExts appear to have valid contents."; - procStrings=("${argProcStrings[@]}"); # Export process command strings - procFileExts=("${argProcFileExts[@]}"); # Export process command strings + procStrings+=("${argProcStrings[@]}"); # Export process command strings + procFileExts+=("${argProcFileExts[@]}"); # Export process command strings vbm "STATUS:$fn:Finished magicParseProcessStrings() function."; } # Validate and save process strings and file extensions to arrays procStrings, procFileExts -magicParseRecipientArgs() { - # Desc: Parses recipient arguments specified by '-r' option - # Input: vars: optionEncrypt, optionRecipients - # arry: argRecPubKeys from processArguments() - # Output: vars: cmd_encrypt, cmd_encrypt_suffix - # arry: recPubKeysValid, recPubKeysValidStatic - # Depends: processArguments(), yell(), vbm(), checkapp(), checkAgePubkey(), validateInput() - local fn recipients +magicParseRecipients() { + # Desc: Parses recipient arguments specified by '-r' or '-R' options + # Usage: magicParseRecipients + # In : vars: optionEncrypt, optionRecArg, optionRecDir + # arry: argRecPubKeys (-r), argRecDir (-R) + # Out: vars: cmd_encrypt, cmd_encrypt_suffix + # Depends: head 8.30, checkapp(), checkAgePubkey(), validateInput() + local fn recipients recipientDir recFileLine updateRecipients + local -a recPubKeysValid candRecPubKeysValid # Save function name fn="${FUNCNAME[0]}"; + vbm "STATUS:$fn:Starting magicParseRecipients() function."; - vbm "STATUS:$fn:Starting magicParseRecipientArgs() function."; - # Check if encryption option active. - if [[ "$optionEncrypt" = "true" ]] && [[ "$optionRecipients" = "true" ]]; then - if checkapp age; then # Check that age is available. - for pubkey in "${argRecPubKeys[@]}"; do # Validate recipient pubkey strings by forming test message - vbm "DEBUG:$fn:Testing pubkey string:$pubkey"; - if checkAgePubkey "$pubkey" && \ - ( validateInput "$pubkey" "ssh_pubkey" || validateInput "$pubkey" "age_pubkey"); then - #### Form age recipient string - recipients="$recipients""-r '$pubkey' "; - vbm "STATUS:$fn:Added pubkey for forming age recipient string:""$pubkey"; - vbm "DEBUG:$fn:recipients:""$recipients"; - #### Add validated pubkey to recPubKeysValid array - recPubKeysValid+=("$pubkey") && vbm "DEBUG:$fn:recPubkeysValid:pubkey added:$pubkey"; - else - yell "ERROR:$fn:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1; - fi; - done - vbm "DEBUG:$fn:Finished processing argRecPubKeys array"; - vbm "STATUS:$fn:Array of validated pubkeys:${recPubKeysValid[*]}"; - recPubKeysValidStatic=("${recPubKeysValid[@]}"); # Save static image of pubkeys validated by this function - - ## Form age command string - cmd_encrypt="age ""$recipients " && vbm "STATUS:$fn:cmd_encrypt:$cmd_encrypt"; - cmd_encrypt_suffix=".age" && vbm "STATUS:$fn:cmd_encrypt_suffix:$cmd_encrypt_suffix"; - else - yell "ERROR:$fn:Encryption enabled but \"age\" not found. Exiting."; exit 1; - fi; - else - cmd_encrypt="tee /dev/null " && vbm "STATUS:$fn:cmd_encrypt:$cmd_encrypt"; - cmd_encrypt_suffix="" && vbm "STATUS:$fn:cmd_encrypt_suffix:$cmd_encrypt_suffix"; - vbm "DEBUG:$fn:Encryption not enabled." - fi; - # Catch case if '-e' is set but '-r' or '-R' is not - if [[ "$optionEncrypt" = "true" ]] && [[ ! "$optionRecipients" = "true" ]]; then + # Catch illegal option combinations + ## Catch case if '-e' is set but neither '-r' nor '-R' is set + if [[ "$optionEncrypt" = "true" ]] && \ + ! { [[ "$optionRecArg" = "true" ]] || [[ "$optionRecDir" = "true" ]]; }; then yell "ERROR:$fn:\\'-e\\' set but no \\'-r\\' or \\'-R\\' set."; exit 1; fi; - # Catch case if '-r' or '-R' set but '-e' is not - if [[ ! "$optionEncrypt" = "true" ]] && [[ "$optionRecipients" = "true" ]]; then + ## Catch case if '-r' or '-R' set but '-e' is not + if [[ ! "$optionEncrypt" = "true" ]] && \ + { [[ "$optionRecArg" = "true" ]] || [[ "$optionRecDir" = "true" ]]; }; then yell "ERROR:$fn:\\'-r\\' or \\'-R\\' set but \\'-e\\' is not set."; exit 1; fi; - vbm "STATUS:$fn:Finished magicParseRecipientArgs() function."; -} # Populate recPubKeysValid with argRecPubKeys; form encryption cmd string and filename suffix -magicParseRecipientDir() { - # Desc: Updates recPubKeysValid with pubkeys in dir specified by '-R' option ("recipient directory") - # Inputs: vars: optionEncrypt, optionRecDir, argRecDir, - # arry: recPubKeysValid - # Outputs: arry: recPubKeysValid - # Depends: processArguments(), yell(), vbm(), validateInput(), checkAgePubkey() - local fn recipientDir recFileLine updateRecipients - declare -a candRecPubKeysValid - - # Save function name - fn="${FUNCNAME[0]}"; - vbm "STATUS:$fn:Starting magicParseRecipientDir() function."; - # Check that '-e' and '-R' set - if [[ "$optionEncrypt" = "true" ]] && [[ "$optionRecDir" = "true" ]]; then - ### Check that argRecDir is a directory. + # Handle no encryption cases + if [[ ! "$optionEncrypt" = "true" ]]; then + cmd_encrypt="cat " && vbm "STATUS:$fn:cmd_encrypt:$cmd_encrypt"; + cmd_encrypt_suffix="" && vbm "STATUS:$fn:cmd_encrypt_suffix:$cmd_encrypt_suffix"; + vbm "DEBUG :$fn:Encryption not enabled."; + return; fi; + + # Handle encryption cases + ## Check age availability + if ! checkapp age; then yell "ERROR:$fn:age not available. Exiting."; exit 1; fi + ## Parse '-r' options: validate and append pubkeys from argRecPubKeys to recPubKeysValid + if [[ "$optionRecArg" = "true" ]]; then + for pubkey in "${argRecPubKeys[@]}"; do # Validate recipient pubkey strings by forming test message + vbm "DEBUG :$fn:Testing pubkey string:$pubkey"; + if checkAgePubkey "$pubkey" && \ + ( validateInput "$pubkey" "ssh_pubkey" || validateInput "$pubkey" "age_pubkey"); then + #### Add validated pubkey to recPubKeysValid array + recPubKeysValid+=("$pubkey") && \ + vbm "DEBUG :$fn:recPubkeysValid:pubkey added:$pubkey"; + else + yell "ERROR:$fn:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1; + fi; + done; + vbm "STATUS:$fn:Finished processing argRecPubKeys array"; + vbm "DEBUG :$fn:Array of validated pubkeys:${recPubKeysValid[*]}"; + fi; + ## Parse '-R' options: validate and append pubkeys in argRecDir to recPubKeysValid + if [[ "$optionRecDir" = "true" ]]; then + ### Check that argRecDir is a directory if [[ -d "$argRecDir" ]]; then - recipientDir="$argRecDir" && vbm "STATUS:$fn:Recipient watch directory detected:\"$recipientDir\""; + recipientDir="$argRecDir" && \ + vbm "STATUS:$fn:Recipient watch directory detected:\"$recipientDir\""; #### Initialize variable indicating outcome of pubkey review unset updateRecipients - #### Add existing recipients - candRecPubKeysValid=("${recPubKeysValidStatic[@]}"); + #### Add existing recipients from '-r' option + candRecPubKeysValid=("${recPubKeysValid[@]}"); #### Parse files in recipientDir for file in "$recipientDir"/*; do ##### Read first line of each file - recFileLine="$(head -n1 "$file")" && vbm "STATUS:$fn:Checking if pubkey:\"$recFileLine\""; + recFileLine="$(head -n1 "$file")" && \ + vbm "STATUS:$fn:Checking if pubkey:\"$recFileLine\""; ##### check if first line is a valid pubkey if checkAgePubkey "$recFileLine" && \ ( validateInput "$recFileLine" "ssh_pubkey" || validateInput "$recFileLine" "age_pubkey"); then ###### T: add candidate pubkey to candRecPubKeysValid - candRecPubKeysValid+=("$recFileLine") && vbm "STATUS:$fn:RecDir pubkey is valid pubkey:\"$recFileLine\""; + candRecPubKeysValid+=("$recFileLine") && \ + vbm "STATUS:$fn:RecDir pubkey is valid pubkey:\"$recFileLine\""; else ###### F: throw warning; - yell "ERROR:$fn:Invalid recipient file detected. Not modifying recipient list." + yell "ERROR:$fn:Invalid recipient file detected. Not modifying recipient list:$recFileLine"; updateRecipients="false"; fi; done - #### Write updated recPubKeysValid array to recPubKeysValid if no failure detected + #### Write candRecPubKeysValid array to recPubKeysValid if no invalid key detected if ! [[ "$updateRecipients" = "false" ]]; then - recPubKeysValid=("${candRecPubKeysValid[@]}") && vbm "STATUS:$fn:Wrote candRecPubkeysValid to recPubKeysValid:\"${recPubKeysValid[*]}\""; + recPubKeysValid=("${candRecPubKeysValid[@]}") && \ + vbm "STATUS:$fn:Wrote candRecPubkeysValid to recPubKeysValid:\"${recPubKeysValid[*]}\""; fi; - else - yell "ERROR:$fn:Recipient directory $argRecDir does not exist. Exiting."; exit 1; fi; fi; - # Handle case if '-R' set but '-e' not set - if [[ ! "$optionEncrypt" = "true" ]] && [[ "$optionRecDir" = "true" ]]; then - yell "ERROR:$fn: \\'-R\\' is set but \\'-e\\' is not set."; fi; - vbm "STATUS:$fn:Finished magicParseRecipientDir() function."; -} # Update recPubKeysValid with argRecDir + + ## Form age recipient string from recPubKeysValid + for pubkey in "${recPubKeysValid[@]}"; do + recipients="$recipients""-r '$pubkey' "; + vbm "STATUS:$fn:Added pubkey for forming age recipient string:""$pubkey"; + vbm "DEBUG :$fn:recipients:""$recipients"; + done; + + ## Output cmd_encrypt, cmd_encrypt_suffix from recipients + cmd_encrypt="age ""$recipients " && vbm "STATUS:$fn:cmd_encrypt:$cmd_encrypt"; + cmd_encrypt_suffix=".age" && vbm "STATUS:$fn:cmd_encrypt_suffix:$cmd_encrypt_suffix"; + + vbm "STATUS:$fn:Finished magicParseRecipients() function."; +} # Sets cmd_encrypt, cmd_encrypt_suffix from -r, -R args magicSetScriptTTL() { #Desc: Sets script_TTL seconds from provided time_element string argument #Usage: magicSetScriptTTL [str time_element] @@ -1234,9 +1167,9 @@ magicProcessWriteBuffer() { # Determine file paths (time is start of buffer period) ## Calculate start time timeBufferStartLong="$(date --date="$bufferTTL seconds ago" --iso-8601=seconds)" && \ - vbm "DEBUG:$fn:timeBufferStartLong:$timeBufferStartLong"; + vbm "DEBUG :$fn:timeBufferStartLong:$timeBufferStartLong"; timeBufferStart="$(dateTimeShort "$timeBufferStartLong" )" && \ - vbm "DEBUG:$fn:timeBufferStart:$timeBufferStart"; # Note start time YYYYmmddTHHMMSS+zzzz (no separators) + vbm "DEBUG :$fn:timeBufferStart:$timeBufferStart"; # Note start time YYYYmmddTHHMMSS+zzzz (no separators) ## Set common basename fileoutBasename="$timeBufferStart""--""$bufferTTL_STR""..""$scriptHostname""$label" && \ vbm "STATUS:$fn:Set fileoutBasename to:$fileoutBasename"; @@ -1281,6 +1214,80 @@ magicProcessWriteBuffer() { vbm "STATUS:$fn:Finished magicProcessWriteBuffer()."; } # Process and Write buffer +showUsage() { + cat <<'EOF' + USAGE: + cmd | bklog [ options ] + + OPTIONS: + -h, --help + Display help information. + --version + Display script version. + -v, --verbose + Display debugging info. + -e, --encrypt + Encrypt output. + -r, --recipient [ string pubkey ] + Specify recipient. May be age or ssh pubkey. + May be specified multiple times for multiple pubkeys. + See https://github.com/FiloSottile/age + -o, --output [ path dir ] + Specify output directory to save logs. This option is required + to save log data. + -p, --process-string [ filter command ] [ output file extension] + Specify how to create and name a processed version of the stdin. + For example, if stdin is 'nmea' location data: + + -p "gpsbabel -i nmea -f - -o gpx -F - " ".gpx" + + This option would cause the stdin to 'bklog' to be piped into + the 'gpsbabel' command, interpreted as 'nmea' data, converted + into 'gpx' format, and then appended to the output tar file + as a file with a '.gpx' extension. + This option may be specified multiple times in order to output + results of multiple different processing methods. + -l, --label [ string ] + Specify a label to be included in all output file names. + Ex: 'location' if stdin is location data. + -w, --store-raw [ file extension ] + Specify file extension of file within output tar that contains + raw stdin data. The default behavior is to always save raw stdin + data in a '.stdin' file. Example usage when 'bklog' receives + 'nmea' data from 'gpspipe -r': + + -w ".nmea" + + Stdin data is saved in a '.nmea' file within the output tar. + -W, --no-store-raw + Do not store raw stdin in output tar. + -c, --compress + Compress output with gzip (before encryption if enabled). + -z, --time-zone + Specify time zone. (ex: "America/New_York") + -t, --temp-dir [path dir] + Specify parent directory for temporary working directory. + Default: "/dev/shm" + -R, --recipient-dir [path dir] + Specify directory containing files whose first lines are + to be interpreted as pubkey strings (see '-r' option). Only + one directory may be specified. + -b, --buffer-ttl [integer] + Specify custom buffer period in seconds (default: 300 seconds) + -B, --script-ttl [time element string] + Specify custom script time-to-live in seconds (default: "day") + Valid values: "day", "hour" + + EXAMPLE: (bash script lines) + $ gpspipe -r | /bin/bash bklog -v -e -c -z "UTC" -t "/dev/shm" \ + -r age1mrmfnwhtlprn4jquex0ukmwcm7y2nxlphuzgsgv8ew2k9mewy3rs8u7su5 \ + -r age1ala848kqrvxc88rzaauc6vc5v0fqrvef9dxyk79m0vjea3hagclswu0lgq \ + -R ~/.config/bklog/recipients -w ".nmea" -b 300 -B "day" \ + -o ~/Sync/Logs -l "location" \ + -p "gpsbabel -i nmea -f - -o gpx -F - " ".gpx" \ + -p "gpsbabel -i nmea -f - -o kml -F - " ".kml" +EOF +} # Display information on how to use this script. main() { # Desc: Main function @@ -1299,10 +1306,8 @@ main() { ## Determine working directory magicInitWorkingDir; # Sets dir_tmp from argTempDirPriority ## Set output encryption and compression option strings - ### React to "-e" and "-r" ("encryption recipients") options - magicParseRecipientArgs; # Updates recPubKeysValid, cmd_encrypt[_suffix] from argRecPubKeys - ### React to "-R" ("recipient directory") option - magicParseRecipientDir; # Updates recPubKeysValid + ### React to "-e", "-r", and "-R" (encryption recipients) options + magicParseRecipients; # Update cmd_encrypt, cmd_encrypt_suffix ### React to "-c" ("compression") option magicParseCompressionArg; # Updates cmd_compress[_suffix] ## React to "-b" and "-B" (custom buffer and script TTL) options @@ -1316,9 +1321,9 @@ main() { ## Set script lifespan (scriptTTL from scriptTTL_TE) magicSetScriptTTL "$scriptTTL_TE"; ## File name substring (ISO-8601 duration from bufferTTL) - bufferTTL_STR="$(timeDuration "$bufferTTL")" && vbm "DEBUG:$fn:bufferTTL_STR:$bufferTTL_STR"; + bufferTTL_STR="$(timeDuration "$bufferTTL")" && vbm "DEBUG :$fn:bufferTTL_STR:$bufferTTL_STR"; ## Init temp working dir - try mkdir "$dir_tmp" && vbm "DEBUG:$fn:Working dir created at dir_tmp:$dir_tmp"; + try mkdir "$dir_tmp" && vbm "DEBUG :$fn:Working dir created at dir_tmp:$dir_tmp"; ## Initialize output tar (set pathout_tar) magicInitCheckTar; @@ -1340,9 +1345,9 @@ main() { # Create dir_tmp if missing if ! [[ -d "$dir_tmp" ]]; then yell "ERROR:$fn:dir_tmp existence failure:$dir_tmp"; - try mkdir "$dir_tmp" && vbm "DEBUG:$fn:Working dir recreated dir_tmp:$dir_tmp"; fi - # Update encryption recipient array - magicParseRecipientDir; # Update recPubKeysValid with argRecDir + try mkdir "$dir_tmp" && vbm "DEBUG :$fn:Working dir recreated dir_tmp:$dir_tmp"; fi + # Update cmd_encrypt, cmd_encrypt_suffix + magicParseRecipients; # Export buffer to asynchronous processing. magicProcessWriteBuffer & unset buffer; # Clear buffer array for next bufferRound