s243a

run-as-puppy

May 12th, 2019
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.68 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. # See Post: http://murga-linux.com/puppy/viewtopic.php?p=1006050#1006050
  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. PUPPY_HOME=$(awk -F: '$1=="puppy" {print $6}' /etc/passwd)
  24. CURDIR=$PWD
  25. if [ $(id -u) -eq 0 ]; then
  26.  
  27.     [ -z "$XAUTHORITY" ] && XAUTHORITY=/root/.Xauthority
  28.     [ -e "$XAUTHORITY" ] && cp $XAUTHORITY $PUPPY_HOME/.Xauthority &&
  29.     chown puppy:puppy $PUPPY_HOME/.Xauthority &&
  30.     export XAUTHORITY=$PUPPY_HOME/.Xauthority
  31.    
  32.     export XDG_CONFIG_HOME=$PUPPY_HOME/.config
  33.     export XDG_CACHE_HOME=$PUPPY_HOME/.cache
  34.     export XDG_DATA_HOME=$PUPPY_HOME/.local/share
  35.     mkdir -p $XDG_DATA_HOME; chown puppy:puppy $XDG_DATA_HOME
  36.     export FATDOG_STATE_DIR=$PUPPY_HOME/.fatdog
  37.  
  38.     set -- "$(eval_safe_quote "$@")"
  39.  
  40.     exec su puppy -s /bin/dash -c '
  41. # try to switch to original directory, unless it is /root
  42. ! [ "'"$CURDIR"'" = /root ] && cd "'"$CURDIR"'"
  43. '"$@"'
  44. '
  45. else
  46.     exec "$@"
  47. fi
Advertisement
Add Comment
Please, Sign In to add comment