Advertisement
s243a

run-as-spot (Fatdog64)

Sep 28th, 2018
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.55 KB | None | 0 0
  1. #!/bin/dash
  2. # dash and ash but not bash!
  3. # generic wrapper to run as spot (when currently running as root)
  4. # (C) James Budiono 2012, 2017
  5. # License: GPL version 3 or later
  6. # 20170920 step: add eval_safe_quote for safe execution.
  7. #
  8. eval_safe_quote() # $@
  9. # Use this shell's 'set' command to consistently quote values with exterior
  10. # Single Quotes (SQ). The SQ character itself is quoted differently according
  11. # to each shell. This implementation is for dash and ash, which quote SQ as
  12. # "'" and ''"'", respectively.
  13. {
  14.     local a stop="$(od -An -N4 -i /dev/urandom)"
  15.     for a; do a="${a}
  16. $stop"; set; done | awk -v Q="'" '# awk, gawk, mawk
  17. f!=1 &&  /^a=/        { printf "%s", substr($0,3); f=1; next }
  18. f==1 && !/^'"$stop"'/ { printf "\n%s", $0; next }
  19. f==1 &&  /^'"$stop"'/ { printf Q" "; f=0; next }
  20. '
  21. }
  22.  
  23. SPOT_HOME=$(awk -F: '$1=="spot" {print $6}' /etc/passwd)
  24. CURDIR=$PWD
  25. if [ $(id -u) -eq 0 ]; then
  26.     [ -z "$XAUTHORITY" ] && XAUTHORITY=/root/.Xauthority
  27.     [ -e "$XAUTHORITY" ] && cp $XAUTHORITY $SPOT_HOME/.Xauthority &&
  28.     chown spot:spot $SPOT_HOME/.Xauthority &&
  29.     export XAUTHORITY=$SPOT_HOME/.Xauthority  
  30.    
  31.     export XDG_CONFIG_HOME=$SPOT_HOME/.config
  32.     export XDG_CACHE_HOME=$SPOT_HOME/.cache
  33.     export XDG_DATA_HOME=$SPOT_HOME/.local/share
  34.     mkdir -p $XDG_DATA_HOME; chown spot:spot $XDG_DATA_HOME
  35.     export FATDOG_STATE_DIR=$SPOT_HOME/.fatdog
  36.  
  37.     set -- "$(eval_safe_quote "$@")"
  38.  
  39.     exec su spot -s /bin/dash -c '
  40. # try to switch to original directory, unless it is /root
  41. ! [ "'"$CURDIR"'" = /root ] && cd "'"$CURDIR"'"
  42. '"$@"'
  43. '
  44. else
  45.     exec "$@"
  46. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement