X-Git-Url: https://zdv2.bktei.com/gitweb/EVA-2020-02.git/blobdiff_plain/80fbd111c9caf56040a50a468421176a34df28ad..3a5ef93a75c25096d46d924712f6facfae8a3d67:/exec/bklog diff --git a/exec/bklog b/exec/bklog index ad75022..c2cb485 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.12"; # Define version of script. +scriptVersion="0.1.21"; # 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; @@ -226,6 +225,64 @@ displayMissing() { #==END Display errors== } # Display missing apps, files, dirs +appendArgTar(){ + # Desc: Writes first argument to temporary file with arguments as options, then appends file to tar + # Usage: appendArgTar "$(echo "Data to be written.")" [name of file to be inserted] [tar path] [temp dir] ([cmd1] [cmd2] [cmd3] [cmd4]...) + # Version: 1.0.6 + # Input: arg1: data to be written + # arg2: file name of file to be inserted into tar + # arg3: tar archive path (must exist first) + # arg4: temporary working dir + # arg5+: command strings (ex: "gpsbabel -i nmea -f - -o kml -F - ") + # Output: file written to disk + # Example: decrypt multiple large files in parallel + # appendArgTar "$(cat /tmp/largefile1.gpg)" "largefile1" $HOME/archive.tar /tmp "gpg --decrypt" & + # appendArgTar "$(cat /tmp/largefile2.gpg)" "largefile2" $HOME/archive.tar /tmp "gpg --decrypt" & + # appendArgTar "$(cat /tmp/largefile3.gpg)" "largefile3" $HOME/archive.tar /tmp "gpg --decrypt" & + # Depends: bash 5, tar 1, yell() + # Ref/Attrib: Using 'eval' to construct command strings https://askubuntu.com/a/476533 + + local fn fileName tarPath tmpDir cmd0 cmd1 cmd2 cmd3 cmd4 + + # Save function name + fn="${FUNCNAME[0]}"; + #yell "DEBUG:STATUS:$fn:Finished appendArgTar()." + + # Set file name + if ! [ -z "$2" ]; then fileName="$2"; else yell "ERROR:$fn:Not enough arguments."; exit 1; fi + + # Check tar path is a file + if [ -f "$3" ]; then tarPath="$3"; else yell "ERROR:$fn:Tar archive arg not a file."; exit 1; fi + + # Check temp dir arg + if ! [ -z "$4" ]; then tmpDir="$4"; else yell "ERROR:$fn:No temporary working dir set."; exit 1; fi + + # Set command strings + if ! [ -z "$5" ]; then cmd1="$5"; else cmd1="cat "; fi # command string 1 + if ! [ -z "$6" ]; then cmd2="$6"; else cmd2="cat "; fi # command string 2 + if ! [ -z "$7" ]; then cmd3="$7"; else cmd3="cat "; fi # command string 3 + if ! [ -z "$8" ]; then cmd4="$8"; else cmd4="cat "; fi # command string 4 + + # Input command + cmd0="echo \"\$1\"" + + # # Debug + # yell "DEBUG:STATUS:$fn:cmd0:$cmd0" + # yell "DEBUG:STATUS:$fn:cmd1:$cmd1" + # yell "DEBUG:STATUS:$fn:cmd2:$cmd2" + # yell "DEBUG:STATUS:$fn:cmd3:$cmd3" + # yell "DEBUG:STATUS:$fn:cmd4:$cmd4" + # yell "DEBUG:STATUS:$fn:fileName:$fileName" + # yell "DEBUG:STATUS:$fn:tarPath:$tarPath" + # yell "DEBUG:STATUS:$fn:tmpDir:$tmpDir" + + # Write to temporary working dir + eval "$cmd0 | $cmd1 | $cmd2 | $cmd3 | $cmd4" > "$tmpDir"/"$fileName"; + + # Append to tar + try tar --append --directory="$tmpDir" --file="$tarPath" "$fileName"; + #yell "DEBUG:STATUS:$fn:Finished appendArgTar()." +} # Append Bash var to file appended to Tar archive appendFileTar(){ # Desc: Appends [processed] file to tar # Usage: appendFileTar [file path] [name of file to be inserted] [tar path] [temp dir] ([process cmd]) @@ -245,7 +302,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 +321,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 +484,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 +836,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() { @@ -861,7 +845,7 @@ magicInitCheckTar() { # input: vars: scriptHostname # output: vars: pathout_tar # depends: Bash 5.0.3, vbm(), dateShort(), checkMakeTar(), magicWriteVersion() - local fn + local fn checkMakeTarES # Save function name fn="${FUNCNAME[0]}"; @@ -871,10 +855,10 @@ magicInitCheckTar() { pathout_tar="$dirOut"/"$(dateShort "$(date --date="$bufferTTL seconds ago" --iso-8601=seconds)")".."$scriptHostname""$label""$cmd_compress_suffix""$cmd_encrypt_suffix".tar && \ vbm "STATUS:$fn:Set pathout_tar to:$pathout_tar"; # Validate pathout_tar as tar. - checkMakeTar "$pathout_tar"; + checkMakeTar "$pathout_tar"; checkMakeTarES="$?"; ## Add VERSION file if checkMakeTar had to create a tar (exited 1) or replace one (exited 2) - vbm "STATUS:$fn:exit status before magicWriteVersion:$?" - if [[ $? -eq 1 ]] || [[ $? -eq 2 ]]; then magicWriteVersion; fi + vbm "STATUS:$fn:exit status before magicWriteVersion:$checkMakeTarES" + if [[ "$checkMakeTarES" -eq 1 ]] || [[ "$checkMakeTarES" -eq 2 ]]; then magicWriteVersion; fi vbm "STATUS:$fn:Finished magicInitCheckTar() function."; } # Initialize tar, set pathout_tar magicParseCompressionArg() { @@ -898,7 +882,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 +972,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 +1028,110 @@ 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 + vbm "STATUS:$fn:argProcStrings:${argProcStrings[*]}" + vbm "STATUS:$fn:argProcStrings:${argProcFileExts[*]}" + 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] @@ -1231,12 +1224,16 @@ magicProcessWriteBuffer() { fn="${FUNCNAME[0]}"; vbm "STATUS:$fn:Started magicProcessWriteBuffer()."; + vbm "DEBUG :$fn:buffer array element count:${#buffer[@]}"; + vbm "DEBUG :$fn:buffer array first element:${buffer[0]}"; + vbm "DEBUG :$fn:buffer array last element :${buffer[-1]}"; + # 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"; @@ -1261,24 +1258,107 @@ magicProcessWriteBuffer() { writeCmd4="$cmd_encrypt"; ## Process buffer and write to dir_tmp + vbm "DEBUG :$fn:fileouts element count:${#fileouts[@]}"; + vbm "DEBUG :$fn:pathouts element count:${#pathouts[@]}"; + vbm "DEBUG :$fn:procStrings element count:${#pathouts[@]}"; + vbm "DEBUG :$fn:fileouts contents:${fileouts[*]}"; + vbm "DEBUG :$fn:pathouts contents:${pathouts[*]}"; + vbm "DEBUG :$fn:procStrings contents:${pathouts[*]}"; for index in "${!pathouts[@]}"; do - writeCmd2="${procStrings[$index]}" - eval "$writeCmd1 | $writeCmd2 | $writeCmd3 | $writeCmd4" >> "${pathouts[$index]}"; + writeCmd2="${procStrings[$index]}"; + writeCmdAll="$writeCmd1 | $writeCmd2 | $writeCmd3 | $writeCmd4" && vbm "STATUS:$fn:Assembled command:\"$writeCmdAll\""; + eval "$writeCmd1 | $writeCmd2 | $writeCmd3 | $writeCmd4" > "${pathouts[$index]}" && vbm "STATUS:$fn:Wrote command output to ${pathouts[$index]}"; done; # Append dir_tmp files to pathout_tar wait; # Wait to avoid collision with older magicProcessWriteBuffer() instances (see https://www.tldp.org/LDP/abs/html/x9644.html ) for index in "${!pathouts[@]}"; do - appendFileTar "${pathouts[$index]}" "${fileouts[$index]}" "$pathout_tar" "$dir_tmp"; + tar --apend --directory="$dir_tmp" --file="$pathout_tar" "${fileouts[$index]}" && \ + vbm "STATUS:$fn:Appended ${pathouts[$index]} to $pathout_tar"; + #appendFileTar "${pathouts[$index]}" "${fileouts[$index]}" "$pathout_tar" "$dir_tmp" && \ done; # Remove secured chunks from dir_tmp for path in "${pathouts[@]}"; do - rm "$path"; + rm "$path" && vbm "STATUS:$fn:Removed:$path"; done; 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 @@ -1297,10 +1377,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 @@ -1314,11 +1392,13 @@ 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; + magicInitCheckTar; + ## Append VERSION file to tar + magicWriteVersion; # Check vital apps, files, dirs if ! checkapp tar && ! checkdir "$dirOut" "dir_tmp"; then @@ -1338,9 +1418,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