qubes-rpc-multiplexer 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/sh
  2. # write stderr to both calling party and local log; be very careful about
  3. # closing file descriptors here - if either stdout or stderr will not be closed
  4. # when service process does the same - service call will hang (waiting for EOF
  5. # on stdout/stderr)
  6. stderr_pipe=/tmp/qrexec-rpc-stderr.$$
  7. mkfifo $stderr_pipe
  8. # tee can't write to file descriptor, nor /proc/self/fd/2 (EXIO on open)
  9. return_stderr_pipe=/tmp/qrexec-rpc-stderr-return.$$
  10. mkfifo $return_stderr_pipe
  11. { cat <$return_stderr_pipe >&2 2>/dev/null; rm -f $return_stderr_pipe; } &
  12. { tee $return_stderr_pipe 2>/dev/null <$stderr_pipe |\
  13. logger -t "$1-$2" >/dev/null 2>&1; rm -f $stderr_pipe; } &
  14. exec 2>$stderr_pipe
  15. QUBES_RPC=/etc/qubes-rpc
  16. LOCAL_QUBES_RPC=/usr/local/etc/qubes-rpc
  17. if ! [ $# = 2 ] ; then
  18. echo "$0: bad argument count, usage: $0 SERVICE-NAME REMOTE-DOMAIN-NAME" >&2
  19. exit 1
  20. fi
  21. export QREXEC_REMOTE_DOMAIN="$2"
  22. export QREXEC_SERVICE_FULL_NAME="$1"
  23. SERVICE_WITHOUT_ARGUMENT="${1%%+*}"
  24. if [ "${QREXEC_SERVICE_FULL_NAME}" != "${SERVICE_WITHOUT_ARGUMENT}" ]; then
  25. export QREXEC_SERVICE_ARGUMENT="${QREXEC_SERVICE_FULL_NAME#*+}"
  26. fi
  27. for CFG_FILE in $LOCAL_QUBES_RPC/"$1" $QUBES_RPC/"$1" \
  28. $LOCAL_QUBES_RPC/"${SERVICE_WITHOUT_ARGUMENT}" \
  29. $QUBES_RPC/"${SERVICE_WITHOUT_ARGUMENT}"; do
  30. if [ -s "$CFG_FILE" ]; then
  31. break
  32. fi
  33. done
  34. if [ -x "$CFG_FILE" ] ; then
  35. # shellcheck disable=SC2086
  36. exec "$CFG_FILE" ${QREXEC_SERVICE_ARGUMENT}
  37. echo "$0: failed to execute handler for" "$1" >&2
  38. exit 1
  39. else
  40. # shellcheck disable=SC2086
  41. exec /bin/sh -- "$CFG_FILE" ${QREXEC_SERVICE_ARGUMENT}
  42. echo "$0: failed to execute handler for" "$1" >&2
  43. exit 1
  44. fi