diff --git a/autogen.sh b/autogen.sh index ac40874..065d654 100755 --- a/autogen.sh +++ b/autogen.sh @@ -19,11 +19,10 @@ case "${1}" in '--reverse') - [[ -x configure ]] && ./configure --enable-maintainer-mode [[ -f Makefile ]] && make maintainer-clean rm -rf build-aux/ rm -f {aclocal.m4,config.h.in,configure,shtool} - find . -type f -iregex '.*Makefile\.in$' -print0 | xargs -r0 rm -f + find . -type f -name 'Makefile.in' -print0 | xargs -r0 rm -f ;; * ) autoreconf --force --install --include 'm4' @@ -31,4 +30,3 @@ case "${1}" in [[ -d autom4te.cache ]] && rm -rf autom4te.cache ;; esac - diff --git a/configure.ac b/configure.ac index f84500d..8e6b48e 100644 --- a/configure.ac +++ b/configure.ac @@ -15,9 +15,9 @@ dnl implied. See the License for the specific language governing permissions dnl and limitations under the License. AC_PREREQ(2.69) -AC_INIT([grenache-cli], [0.4.1], [davide@bitfinex.com]) +AC_INIT([grenache-cli], [0.5.0], [davide@bitfinex.com]) -AC_SUBST([VERSION], [0.4.1]) +AC_SUBST([VERSION], [0.5.0]) AC_SUBST([SB], [`$srcdir/shtool echo -n -e %B`]) AC_SUBST([EB], [`$srcdir/shtool echo -n -e %b`]) @@ -36,7 +36,7 @@ AC_CONFIG_AUX_DIR([build-aux]) AC_CONFIG_SRCDIR([include/io.h]) AC_CONFIG_HEADERS([config.h]) -AC_CANONICAL_TARGET +AC_CANONICAL_HOST AM_INIT_AUTOMAKE([foreign no-dist-gzip dist-xz]) AX_IS_RELEASE([git-directory]) @@ -240,5 +240,5 @@ AC_MSG_RESULT AC_MSG_RESULT AC_MSG_RESULT([==================================================]) AX_MSG_OPTION([The binary will be optimized using], [${ax_cv_gcc_archflag}]) -AX_MSG_OPTION([This version was configured for], [${target}]) +AX_MSG_OPTION([This version was configured for], [${host_cpu}-${host_os}]) AC_MSG_RESULT diff --git a/src/grc-keygen.c b/src/grc-keygen.c index cb861fe..64bfebe 100644 --- a/src/grc-keygen.c +++ b/src/grc-keygen.c @@ -75,7 +75,7 @@ int main(void) { } #ifdef HAVE_GETENTROPY - if (getentropy(seed, ED25519_SEED_SIZE) != ED25519_SEED_SIZE) { + if (getentropy(seed, ED25519_SEED_SIZE) != 0) { #else if (ed25519_create_seed(seed) != 0) { #endif diff --git a/src/grenache-get.in b/src/grenache-get.in index c56c517..794a99d 100644 --- a/src/grenache-get.in +++ b/src/grenache-get.in @@ -17,12 +17,13 @@ # and limitations under the License. # ############################################################################ -ME="${BASH_SOURCE[0]}" -WHOAMI="$(@READLINK@ -snf "${ME}")" -ARGV=$(@GETOPT@ -o 'g:hip:trvw' --long 'grape:,help,id,port:,tls,raw,value,write' -n "${ME##*/}" -- "$@") || exit 1 +readonly ME="${BASH_SOURCE[0]}" +readonly WHOAMI="$(@READLINK@ -snf "${ME}")" +readonly ARGV=$(@GETOPT@ -o 'g:hip:trvVw' --long 'grape:,help,id,port:,tls,raw,value,version,write' -n "${ME##*/}" -- "$@") || exit 1 TLS='' PORT=30002 +EXIT_CODE=1 HOSTNAME='127.0.0.1' QUERY='select(.v) | .v' @@ -44,6 +45,7 @@ Usage: -w, --write Get the write token -h, --help Show this message + -V, --version Show version information The order of the specified options is not relevant. _EOF @@ -51,6 +53,20 @@ _EOF exit 1 } +# Show program's version. +function show_version { + @CAT@ <<_EOF +@PACKAGE_NAME@ (@host_cpu@-@host_os@) @PACKAGE_VERSION@ +Copyright (C) 2017, 2018 Davide Scola <@PACKAGE_BUGREPORT@> +This is free software; see the source for copying conditions. There is +NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR +PURPOSE. + +_EOF + + exit 1 +} + while true; do case "$1" in @@ -95,6 +111,9 @@ while true; do -v | --value ) QUERY='select(.v) | .v'; shift 1 ;; + -V | --version ) + show_version; shift 1 + ;; -w | --write ) QUERY='select(.token) | .token'; shift 1 ;; @@ -113,67 +132,70 @@ done exit 1 } -exec {debug}>>/dev/null +exec {stderr}>&2 + +[[ x"${GRENACHE_CLI_DEBUG:+set}" != xset ]] && { + exec 2>/dev/null +} for hash in "${@}" do [[ "${hash}" =~ ^[a-fA-F0-9]{40}$ ]] || { - echo "${ME##*/}: error: invalid hash provided: ${hash}." >&2 - - exec {debug}>&- - exit 1 + echo "${ME##*/}: warning: invalid hash provided: ${hash}." >&"${stderr}" + continue } JSON="$(@CURL@ -qK "${HOME}/.grenache-cli/grenache-cli.conf" -A '@PACKAGE@/@PACKAGE_VERSION@' "http${TLS:+s}://${HOSTNAME}:${PORT}/get" < <( \ @JQ@ -cnM \ --arg 'data' "${hash}" \ --arg 'rid' "$(@UUIDGEN@)" \ - '{ "data": $data, "rid": $rid }' 2>&"${debug}" \ - ) 2>&"${debug}")" + '{ "data": $data, "rid": $rid }' \ + ))" - @JQ@ -e 'has("k")' >/dev/null 2>&"${debug}" <<<"${JSON}" && { + [[ -z "${JSON}" ]] && { + echo "${ME##*/}: warning: hash ${hash} not found." >&"${stderr}" + continue + } + + @JQ@ -e 'has("k")' >/dev/null <<<"${JSON}" && { CHALLENGE='' - @JQ@ -e 'has("salt")' >/dev/null 2>&"${debug}" <<<"${JSON}" && { - CHALLENGE+="$(@JQ@ -cMjr '"4:salt\(.salt | length):\(.salt)"' 2>&"${debug}" <<<"${JSON}")" + @JQ@ -e 'has("salt")' >/dev/null <<<"${JSON}" && { + CHALLENGE+="$(@JQ@ -j '"4:salt\(.salt | length):\(.salt)"' <<<"${JSON}")" } - CHALLENGE+="$(@JQ@ -cMjr '"3:seqi" + "\(.seq)" + "e1:v" + "\(.v | length):\(.v)"' 2>&"${debug}" <<<"${JSON}")" + CHALLENGE+="$(@JQ@ -j '"3:seqi" + "\(.seq)" + "e1:v" + "\(.v | length):\(.v)"' <<<"${JSON}")" ${WHOAMI%/*}/grc-verify "${CHALLENGE}" \ @PKEY_FILENO@< <( \ - @JQ@ -cMr 'select(.k) | .k' <<<"${JSON}" 2>&"${debug}" + @JQ@ -r 'select(.k) | .k' <<<"${JSON}" ) \ @SIGN_FILENO@< <( \ - @JQ@ -cMr 'select(.sig) | .sig' <<<"${JSON}" 2>&"${debug}" - ) >/dev/null 2>&"${debug}" || { - echo "${ME##*/}: error: cannot verify challenge buffer." >&2 - - exec {debug}>&- - exit 1 + @JQ@ -r 'select(.sig) | .sig' <<<"${JSON}" + ) >/dev/null || { + echo "${ME##*/}: warning: cannot verify challenge buffer." >&"${stderr}" + continue } CHECK="$(@SHA1SUM@ < <( \ printf '%s' \ - "$(@XXD@ -r -p < <( @JQ@ -cMr 'select(.k) | .k' <<<"${JSON}" 2>&"${debug}" ))" \ - "$(@JQ@ -cMr 'select(.salt) | .salt' <<<"${JSON}" 2>&"${debug}")" - ) 2>&"${debug}" \ - ) 2>&"${debug}")" + "$(@XXD@ -r -p < <( @JQ@ -r 'select(.k) | .k' <<<"${JSON}" ))" \ + "$(@JQ@ -r 'select(.salt) | .salt' <<<"${JSON}")" + ))" } || { CHECK="$(@SHA1SUM@ < <( \ - @JQ@ -cMjr 'select(.v) | "\(.v | length):\(.v)"' <<<"${JSON}" 2>&"${debug}" - ) 2>&"${debug}")" + @JQ@ -j 'select(.v) | "\(.v | length):\(.v)"' <<<"${JSON}" + ))" } [[ "x${hash}" = "x${CHECK%% *}" ]] || { - echo "${ME##*/}: error: unexpected value." >&2 - - exec {debug}>&- - exit 1 + echo "${ME##*/}: warning: unexpected value." >&"${stderr}" + continue } - @JQ@ -cMr "${QUERY}" <<<"${JSON}" 2>&"${debug}" + @JQ@ -r "${QUERY}" <<<"${JSON}" \ + && EXIT_CODE=0 done -exec {debug}>&- -exit 0 +exec {stderr}>&- +exit "${EXIT_CODE}" diff --git a/src/grenache-keygen.in b/src/grenache-keygen.in index 8537705..46a2f85 100644 --- a/src/grenache-keygen.in +++ b/src/grenache-keygen.in @@ -17,9 +17,9 @@ # and limitations under the License. # ############################################################################ -ME="${BASH_SOURCE[0]}" -WHOAMI="$(@READLINK@ -snf "${ME}")" -ARGV=$(@GETOPT@ -o 'fh' --long 'force,help' -n "${ME##*/}" -- "$@") || exit 1 +readonly ME="${BASH_SOURCE[0]}" +readonly WHOAMI="$(@READLINK@ -snf "${ME}")" +readonly ARGV=$(@GETOPT@ -o 'fhV' --long 'force,help,version' -n "${ME##*/}" -- "$@") || exit 1 OVERWRITE='no' @@ -35,6 +35,7 @@ Usage: -f, --force Overwrite existing keypair -h, --help Show this message + -V, --version Show version information The order of the specified options is not relevant. _EOF @@ -42,6 +43,20 @@ _EOF exit 1 } +# Show program's version. +function show_version { + @CAT@ <<_EOF +@PACKAGE_NAME@ (@host_cpu@-@host_os@) @PACKAGE_VERSION@ +Copyright (C) 2017, 2018 Davide Scola <@PACKAGE_BUGREPORT@> +This is free software; see the source for copying conditions. There is +NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR +PURPOSE. + +_EOF + + exit 1 +} + while true; do case "$1" in @@ -51,6 +66,9 @@ while true; do -h | --help ) show_help; shift 1 ;; + -V | --version ) + show_version; shift 1 + ;; -- ) shift; break ;; * ) break ;; esac @@ -58,42 +76,54 @@ done umask 0077 -exec {debug}>>/dev/null +exec {stderr}>&2 + +[[ x"${GRENACHE_CLI_DEBUG:+set}" != xset ]] && { + exec 2>/dev/null +} [[ "x${OVERWRITE}" = 'xno' ]] && { set -o noclobber } -@MKDIR@ "${HOME}/.grenache-cli" 2>&"${debug}" && { - @CAT@ > "${HOME}/.grenache-cli/grenache-cli.conf" 2>&"${debug}" <<_EOF +@MKDIR@ "${HOME}/.grenache-cli" && { + @CAT@ > "${HOME}/.grenache-cli/grenache-cli.conf" <<_EOF # # General # silent -compress +no-buffer +max-time 10 +connect-timeout 5 # # Request # -data "@-" +compressed +tcp-nodelay +tcp-fastopen +request POST +data-binary = "@-" # # Headers # -header = "Connection:close" -header = "Accept:application/json" -header = "Content-Type:application/json; charset=UTF-8" +header = "Connection: close" +header = "Accept: application/json" +header = "Content-Type: application/json; charset=UTF-8" _EOF } -${WHOAMI%/*}/grc-keygen 2>&"${debug}" \ +${WHOAMI%/*}/grc-keygen \ @SKEY_FILENO@> "${HOME}/.grenache-cli/key" \ @PKEY_FILENO@> "${HOME}/.grenache-cli/key.pub" || { \ - echo "${ME##*/}: error: cannot create new keypair." >&2 + echo "${ME##*/}: error: cannot create new keypair." >&"${stderr}" + + exec {stderr}>&- exit 1 } echo "${ME##*/}: keypair generated successfully." -exec {debug}>&- +exec {stderr}>&- exit 0 diff --git a/src/grenache-lookup.in b/src/grenache-lookup.in index 6ad7be0..8116ad0 100644 --- a/src/grenache-lookup.in +++ b/src/grenache-lookup.in @@ -17,9 +17,9 @@ # and limitations under the License. # ############################################################################ -ME="${BASH_SOURCE[0]}" -WHOAMI="$(@READLINK@ -snf "${ME}")" -ARGV=$(@GETOPT@ -o 'afg:hlp:rt' --long 'all,first,grape:,help,last,port:,random,tls' -n "${ME##*/}" -- "$@") || exit 1 +readonly ME="${BASH_SOURCE[0]}" +readonly WHOAMI="$(@READLINK@ -snf "${ME}")" +readonly ARGV=$(@GETOPT@ -o 'afg:hlp:rtV' --long 'all,first,grape:,help,last,port:,random,tls,version' -n "${ME##*/}" -- "$@") || exit 1 TLS='' PORT=30002 @@ -50,6 +50,7 @@ Usage: -t, --tls Enable TLS -h, --help Show this message + -V, --version Show version information The order of the specified options is not relevant. _EOF @@ -57,6 +58,20 @@ _EOF exit 1 } +# Show program's version. +function show_version { + @CAT@ <<_EOF +@PACKAGE_NAME@ (@host_cpu@-@host_os@) @PACKAGE_VERSION@ +Copyright (C) 2017, 2018 Davide Scola <@PACKAGE_BUGREPORT@> +This is free software; see the source for copying conditions. There is +NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR +PURPOSE. + +_EOF + + exit 1 +} + while true; do case "$1" in @@ -104,6 +119,9 @@ while true; do -t | --tls ) TLS='yes'; shift 1 ;; + -V | --version ) + show_version; shift 1 + ;; -- ) shift; break ;; * ) break ;; esac @@ -119,21 +137,25 @@ done exit 1 } -exec {debug}>>/dev/null +exec {stderr}>&2 + +[[ x"${GRENACHE_CLI_DEBUG:+set}" != xset ]] && { + exec 2>/dev/null +} for service in "${@}" do - NODES=( $(@JQ@ -Mr '@tsv' < <( \ + NODES=( $(@JQ@ -r '@tsv' < <( \ @CURL@ -qK "${HOME}/.grenache-cli/grenache-cli.conf" -A '@PACKAGE@/@PACKAGE_VERSION@' "http${TLS:+s}://${HOSTNAME}:${PORT}/lookup" < <( \ @JQ@ -cnM \ --arg 'data' "${service}" \ --arg 'rid' "$(@UUIDGEN@)" \ - '{ "data": $data, "rid": $rid }' 2>&"${debug}" \ - ) 2>&"${debug}" \ - ) 2>&"${debug}") ) + '{ "data": $data, "rid": $rid }' \ + ) \ + )) ) [[ ${#NODES[@]} -eq 0 ]] && { \ - echo "${ME##*/}: warning: service ${service} not available." >&2 + echo "${ME##*/}: warning: service ${service} not available." >&"${stderr}" continue } @@ -154,5 +176,5 @@ do && EXIT_CODE=0 done -exec {debug}>&- +exec {stderr}>&- exit "${EXIT_CODE}" diff --git a/src/grenache-put.in b/src/grenache-put.in index 716b9da..ad2348b 100644 --- a/src/grenache-put.in +++ b/src/grenache-put.in @@ -17,9 +17,9 @@ # and limitations under the License. # ############################################################################ -ME="${BASH_SOURCE[0]}" -WHOAMI="$(@READLINK@ -snf "${ME}")" -ARGV=$(@GETOPT@ -o 'c:g:hm:n:p:s:t' --long 'cas:,grape:,help,mutable,number:,port:,salt:,tls' -n "${ME##*/}" -- "$@") || exit 1 +readonly ME="${BASH_SOURCE[0]}" +readonly WHOAMI="$(@READLINK@ -snf "${ME}")" +readonly ARGV=$(@GETOPT@ -o 'c:g:hm:n:p:s:tV' --long 'cas:,grape:,help,mutable,number:,port:,salt:,tls,version' -n "${ME##*/}" -- "$@") || exit 1 TLS='' CAS='' @@ -47,6 +47,7 @@ Usage: -t, --tls Enable TLS -h, --help Show this message + -V, --version Show version information The order of the specified options is not relevant. _EOF @@ -54,6 +55,20 @@ _EOF exit 1 } +# Show program's version. +function show_version { + @CAT@ <<_EOF +@PACKAGE_NAME@ (@host_cpu@-@host_os@) @PACKAGE_VERSION@ +Copyright (C) 2017, 2018 Davide Scola <@PACKAGE_BUGREPORT@> +This is free software; see the source for copying conditions. There is +NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR +PURPOSE. + +_EOF + + exit 1 +} + while true; do case "$1" in @@ -131,6 +146,9 @@ while true; do -t | --tls ) TLS='yes'; shift 1 ;; + -V | --version ) + show_version; shift 1 + ;; -- ) shift; break ;; * ) break ;; esac @@ -146,12 +164,15 @@ done exit 1 } -exec {debug}>>/dev/null - VALUE="${*}" +exec {stderr}>&2 + +[[ x"${GRENACHE_CLI_DEBUG:+set}" != xset ]] && { + exec 2>/dev/null +} [[ "${#VALUE}" -gt 1000 ]] && { - echo "${ME##*/}: warning: value is too big and it will be truncated." >&2 + echo "${ME##*/}: warning: value is too big and it will be truncated." >&"${stderr}" VALUE="${VALUE:0:1000}" } @@ -166,7 +187,7 @@ ARGUMENTS=( PROPERTIES+=', "seq": $seq | tonumber, "k": $k' ARGUMENTS+=( --arg 'seq' "${SEQUENCE}" - --arg 'k' "$(@CAT@ ${HOME}/.grenache-cli/key.pub 2>&"${debug}")" + --arg 'k' "$(@CAT@ ${HOME}/.grenache-cli/key.pub)" ) [[ "x${SALT}" != 'x' ]] && { @@ -183,13 +204,12 @@ ARGUMENTS=( SIGNATURE="$(${WHOAMI%/*}/grc-sign "${CHALLENGE}" \ @SKEY_FILENO@< "${HOME}/.grenache-cli/key" \ @PKEY_FILENO@< "${HOME}/.grenache-cli/key.pub" \ - 2>&"${debug}" )" [[ "x${SIGNATURE}" = 'x' ]] && { - echo "${ME##*/}: error: cannot sign challenge buffer." >&2 + echo "${ME##*/}: error: cannot sign challenge buffer." >&"${stderr}" - exec {debug}>&- + exec {stderr}>&- exit 1 } @@ -206,40 +226,40 @@ ARGUMENTS=( } } -HASH="$(@JQ@ -cMr '.' < <( +HASH="$(@JQ@ -r '.' < <( @CURL@ -qK "${HOME}/.grenache-cli/grenache-cli.conf" -A '@PACKAGE@/@PACKAGE_VERSION@' "http${TLS:+s}://${HOSTNAME}:${PORT}/put" < <( \ @JQ@ -cnM \ "${ARGUMENTS[@]}" \ --arg 'rid' "$(@UUIDGEN@)" \ - "{ "data": { ${PROPERTIES} }, "rid": \$rid }" 2>&"${debug}" \ - ) 2>&"${debug}" \ -) 2>&"${debug}")" + "{ \"data\": { ${PROPERTIES} }, \"rid\": \$rid }" \ + ) \ +))" [[ "${HASH}" =~ ^[a-fA-F0-9]{40}$ ]] || { - echo "${ME##*/}: error: ${HASH:-empty response received from Grape}." >&2 + echo "${ME##*/}: error: ${HASH:-empty response received from Grape}." >&"${stderr}" - exec {debug}>&- + exec {stderr}>&- exit 1 } [[ "x${MUTABLE}" == 'xyes' ]] && { CHECK="$(@SHA1SUM@ < <( \ - printf '%s%s' "$(@XXD@ -r -p < <(@CAT@ ${HOME}/.grenache-cli/key.pub 2>&"${debug}") 2>&"${debug}")" "${SALT}" 2>&"${debug}" \ - ) 2>&"${debug}")" + printf '%s%s' "$(@XXD@ -r -p < <(@CAT@ ${HOME}/.grenache-cli/key.pub))" "${SALT}" \ + ))" } || { CHECK="$(@SHA1SUM@ < <( \ - printf '%d:%s' "${#VALUE}" "${VALUE}" 2>&"${debug}" \ - ) 2>&"${debug}")" + printf '%d:%s' "${#VALUE}" "${VALUE}" \ + ))" } [[ "x${HASH}" = "x${CHECK%% *}" ]] || { - echo "${ME##*/}: error: unexpected hash value." >&2 + echo "${ME##*/}: error: unexpected hash value." >&"${stderr}" - exec {debug}>&- + exec {stderr}>&- exit 1 } echo "${HASH}" -exec {debug}>&- +exec {stderr}>&- exit 0