From a1cb8ea96076c95ff5af844e1e9d983e060c30b2 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Wed, 16 Jun 2021 08:23:55 +0900 Subject: [PATCH] include/ipc: Clean up ipc_msg API The ipc module currently doesn't provide functions to get all fields contained within a message. Further, signature-related functions don't return an error if the message is invalid or the signature could not be retrieved for another reason. This commit adds the missing functions to the public API of the ipc module and fixes the behavior of signature related functions so the caller can correctly determine if an error has occurred. The following functions have been added: - ipc_msg_get_version() - ipc_msg_get_signature() - ipc_msg_get_signer_name() - ipc_msg_get_signer_email() - ipc_msg_get_signer_key() --- include/ipc.sh | 137 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 102 insertions(+), 35 deletions(-) diff --git a/include/ipc.sh b/include/ipc.sh index c289a91..7e452bf 100644 --- a/include/ipc.sh +++ b/include/ipc.sh @@ -61,8 +61,13 @@ _ipc_msg_get_signature() { local data local signature - data=$(_ipc_msg_get "$msg" "data") - signature=$(_ipc_msg_get "$msg" "signature") + if ! data=$(_ipc_msg_get "$msg" "data"); then + return 2 + fi + + if ! signature=$(_ipc_msg_get "$msg" "signature"); then + return 2 + fi if ! gpg --verify <(base64 -d <<< "$signature") <(echo "$data") 2>&1; then return 1 @@ -90,7 +95,7 @@ _ipc_msg_version_supported() { local -i version - if ! version=$(_ipc_msg_get "$msg" "version"); then + if ! version=$(ipc_msg_get_version "$msg"); then log_error "Could not get version from message" return 1 fi @@ -117,7 +122,7 @@ ipc_msg_validate() { return 0 } -ipc_msg_get_signature_info() { +_ipc_msg_get_signature_info() { local msg="$1" local signature @@ -138,9 +143,18 @@ ipc_msg_get_signature_info() { sig_email="(unknown)" sig_key="(unknown)" - if signature=$(_ipc_msg_get_signature "$msg"); then - sig_valid="good" - fi + signature=$(_ipc_msg_get_signature "$msg") + case "$?" in + 0) + sig_valid="good" + ;; + 1) + sig_valid="bad" + ;; + *) + return 1 + ;; + esac if [[ "$signature" =~ $sig_nameregex ]]; then sig_name="${BASH_REMATCH[1]}" @@ -155,26 +169,6 @@ ipc_msg_get_signature_info() { return 0 } -ipc_msg_get_signing_key() { - local msg="$1" - - local signature - local keyregex - - keyregex='([0-9a-fA-F]{32,})' - - if ! signature=$(_ipc_msg_get_signature "$msg"); then - return 1 - fi - - if [[ "$signature" =~ $keyregex ]]; then - echo "${BASH_REMATCH[1]}" - return 0 - fi - - return 1 -} - ipc_msg_dump() { local msg="$1" @@ -256,6 +250,19 @@ _ipc_msg_new() { return 0 } +ipc_msg_get_version() { + local msg="$1" + + local version + + if ! version=$(_ipc_msg_get "$msg" "version"); then + return 1 + fi + + echo "$version" + return 0 +} + ipc_msg_get_source() { local msg="$1" @@ -282,6 +289,32 @@ ipc_msg_get_destination() { return 0 } +ipc_msg_get_user() { + local msg="$1" + + local user + + if ! user=$(_ipc_msg_get "$msg" "user"); then + return 1 + fi + + echo "$user" + return 0 +} + +ipc_msg_get_timestamp() { + local msg="$1" + + local timestamp + + if ! timestamp=$(_ipc_msg_get "$msg" "timestamp"); then + return 1 + fi + + echo "$timestamp" + return 0 +} + ipc_msg_get_data() { local msg="$1" @@ -300,29 +333,63 @@ ipc_msg_get_data() { return 0 } -ipc_msg_get_user() { +ipc_msg_get_signature() { local msg="$1" - local user + local signature - if ! user=$(_ipc_msg_get "$msg" "user"); then + if ! signature=$(_ipc_msg_get "$msg" "signature"); then return 1 fi - echo "$user" + echo "$signature" return 0 } -ipc_msg_get_timestamp() { +ipc_msg_get_signer_name() { local msg="$1" - local timestamp + local info + local fields - if ! timestamp=$(_ipc_msg_get "$msg" "timestamp"); then + if ! info=$(_ipc_msg_get_signature_info "$msg"); then return 1 fi - echo "$timestamp" + read -ra fields <<< "$info" + echo "${fields[@]:3}" + return 0 +} + +ipc_msg_get_signer_email() { + local msg="$1" + + local info + local fields + + if ! info=$(_ipc_msg_get_signature_info "$msg"); then + return 1 + fi + + read -ra fields <<< "$info" + + echo "${fields[2]}" + return 0 +} + +ipc_msg_get_signer_key() { + local msg="$1" + + local info + local fields + + if ! info=$(_ipc_msg_get_signature_info "$msg"); then + return 1 + fi + + read -ra fields <<< "$info" + + echo "${fields[1]}" return 0 } -- 2.47.3