taler-exchange-helper-measure-clear-continue (3791B)
1 #!/bin/bash 2 # 3 # This file is part of TALER 4 # Copyright (C) 2024, 2025 Taler Systems SA 5 # 6 # TALER is free software; you can redistribute it and/or modify it under the 7 # terms of the GNU General Public License as published by the Free Software 8 # Foundation; either version 3, or (at your option) any later version. 9 # 10 # TALER is distributed in the hope that it will be useful, but WITHOUT ANY 11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 13 # 14 # You should have received a copy of the GNU General Public License along with 15 # TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/license> 16 # 17 18 # Hard error reporting on. 19 set -eu 20 21 22 # Exit, with error message (hard failure) 23 function exit_fail() { 24 echo " FAIL: " "$@" >&2 25 EXIT_STATUS=1 26 exit "$EXIT_STATUS" 27 } 28 29 CONF="$HOME/.config/taler-exchange.conf" 30 VERBOSE=0 31 32 while getopts 'ac:hirvV' OPTION; 33 do 34 case "$OPTION" in 35 a) 36 exit 0 37 ;; 38 c) 39 # shellcheck disable=SC2034 40 CONF="$OPTARG" 41 ;; 42 h) 43 echo "This is a KYC measure program that clears a measure from the rule set and continues with another AML program, all controlled via the context." 44 echo 'Supported options:' 45 echo ' -a -- show required attributes' 46 # shellcheck disable=SC2016 47 echo ' -c $CONF -- set configuration' 48 echo ' -h -- print this help' 49 echo ' -i -- show required inputs' 50 echo ' -r -- show required context' 51 echo ' -v -- show version' 52 echo ' -V -- be verbose' 53 exit 0 54 ;; 55 i) 56 # Need context and current_rules. 57 echo "context" 58 echo "current_rules" 59 exit 0 60 ;; 61 r) 62 # Context for AML program to run next 63 echo "next_context" 64 # Binary name of AML program to run next 65 echo "exec_name" 66 # Which measure to clear? 67 echo "clear_measure" 68 exit 0 69 ;; 70 v) 71 echo "$0 v0.0.0" 72 exit 0 73 ;; 74 V) 75 VERBOSE=1 76 ;; 77 ?) 78 exit_fail "Unrecognized command line option" 79 ;; 80 esac 81 done 82 83 if [ 1 = "$VERBOSE" ] 84 then 85 echo "Running $0" 1>&2 86 fi 87 88 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlProgramInput 89 # for the full JSON with possible inputs. 90 91 # First, extract inputs we need 92 INPUTS=$(jq '{"current_rules":.current_rules,"attributes":.attributes,"context":.context}') 93 94 # Get current rules. 95 CURRENT_RULES=$(echo "$INPUTS" | jq '.current_rules // null') 96 # Get context values. 97 J_NEXT_CONTEXT=$(echo "$INPUTS" | jq '.context.next_context // {}') 98 EXEC_NAME=$(echo "$INPUTS" | jq -r '.context.exec_name') 99 CLEAR_MEASURE=$(echo "$INPUTS" | jq '.context.clear_measure // null') 100 101 # Remove matching measure from current rules. 102 J_NEW_RULES=$(echo "$CURRENT_RULES" | jq --argjson cm "$CLEAR_MEASURE" '(.rules[] |= if (.measures[0]==$cm) then del(.) else . end)') 103 104 echo "Passing new rules ${J_NEW_RULES} to ${EXEC_NAME}." 1>&2 105 106 # FIXME: we might want to restrict EXEC_NAME to binaries 107 # with a certain prefix and/or even validate that it is 108 # an AML program in some 'approved' list. Right now, an 109 # AML officer (reasonably trusted...) could basically 110 # run any binary on the server here... 111 112 # Finally, pass the new rules as input to the AML program '$EXEC_NAME'. 113 jq -n \ 114 --argjson nc "$J_NEXT_CONTEXT" \ 115 --argjson nr "$J_NEW_RULES" \ 116 '{"current_rules":$nr,"context":$nc}' \ 117 | exec "${EXEC_NAME}"