uninstall_tor_bundle.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/bin/sh
  2. #
  3. # not a psueber-pretty uninstall script for the Tor bundle package
  4. # . this currently leaves ~/.tor, /var/log/tor and empty
  5. # directories /Library/Tor, /Library/Privoxy (see comment below)
  6. # . this relies on the fact that the startup items directories (if any)
  7. # will be named the same as the package name (ie. Tor)
  8. #
  9. #
  10. # version history
  11. # initial version - 21 may, 2005 - loki der quaeler
  12. #
  13. #
  14. # comments
  15. # loki: because of the way the Tor package installs itself (the root directory
  16. # is the filesystem root, as opposed to the much nicer way that privoxy
  17. # installs itself with the privoxy home (/Library/Privoxy) as its
  18. # install root directory), i thought it more prudent to leave empty
  19. # directories laying around rather than try to incorrectly intuit from
  20. # the bom contents what directories should exist and which ones could be
  21. # deleted (ie, the Tor package has /Library listed in its bom --
  22. # obviously this is a Bad Thing(tm) to delete).
  23. # + when the Tor installer is changed, this uninstaller could be modified.
  24. # loki: /bin/ps, when run from a terminal window in osX, restricts information
  25. # based on the width of the window. an 80 col window will stupidly cause
  26. # the grep search for the privoxy pid to not find the pid, whereas the grep
  27. # in a wider window succeeds. consider using killall. in the meantime,
  28. # advise uninstall runners to drag wide their terminal window.. ugh
  29. #
  30. ### this is the location of a file which contains all the actual package names
  31. ## (ie "Tor", "torstartup", ...) the list should be new-line-delimited.
  32. PACKAGE_LIST_SRC=./package_list.txt
  33. ### this is the name of the user created in the install process of Tor
  34. TOR_USER=_tor
  35. ### these should be constant across all osX installs (so leave them be)
  36. STARTUP_ITEMS_DIR=/Library/StartupItems
  37. PKG_RCPT_BASE_DIR=/Library/Receipts
  38. BOM_INTERMEDIATE_DIR=Contents/Resources
  39. INFO_INTERMEDIATE_DIR=$BOM_INTERMEDIATE_DIR/English.lproj
  40. TEMP_BOM_CONTENTS=/tmp/tor_uninst_scratch
  41. ### make sure the script is being run as root, barf if not
  42. if [ "`whoami`" != "root" ]; then
  43. echo "Must be root to run the uninstall script."
  44. exit -1
  45. fi
  46. ### check to see if tor is currently running, kill it if it is
  47. ## we grep on 'Tor/tor ' because 'tor' is too common (like in 'directory')
  48. ## -- this relies on the fact that tor has been started with command
  49. ## line arguments.. :-/
  50. TOR_PID=`ps -uax | grep 'Tor/tor ' | grep -v grep | awk '{print $2;}'`
  51. if [ ${#TOR_PID} -gt 0 ]; then
  52. echo ". Killing currently running tor process, pid is $TOR_PID"
  53. kill -9 $TOR_PID
  54. else
  55. echo ". tor process appears to already be stopped"
  56. fi
  57. ### check to see if privoxy is currently running, kill it if it is
  58. PRIVOXY_PID=`ps -uax | grep privoxy | grep -v grep | awk '{print $2;}'`
  59. if [ ${#PRIVOXY_PID} -gt 0 ]; then
  60. echo ". Killing currently running privoxy process, pid is $PRIVOXY_PID"
  61. kill -9 $PRIVOXY_PID
  62. else
  63. echo ". privoxy process appears to already be stopped"
  64. fi
  65. ## grab each package name from the package list file
  66. while read LINE; do
  67. if [ ${#LINE} -gt 0 ]; then
  68. PACKAGE_NAME=$LINE.pkg
  69. PACKAGE_PATH=$PKG_RCPT_BASE_DIR/$PACKAGE_NAME
  70. echo ". Uninstalling $PACKAGE_NAME"
  71. if [ ! -d $PACKAGE_PATH ]; then
  72. echo " . No receipt exists for this package -- skipping."
  73. continue
  74. fi
  75. ## get rid of the startup item if it exists
  76. STARTUP_DIR=$STARTUP_ITEMS_DIR/$LINE
  77. if [ -d $STARTUP_DIR ]; then
  78. echo " . Deleting startup item $STARTUP_DIR"
  79. rm -rf $STARTUP_DIR
  80. fi
  81. ## determine the root directory of the the relative paths specified in the bom
  82. DEFAULT_LOC=`grep DefaultLocation $PACKAGE_PATH/$INFO_INTERMEDIATE_DIR/$LINE.info | awk '{print $2;}'`
  83. if [ ${#DEFAULT_LOC} -eq 0 ]; then
  84. echo "!! Could not find default location for $LINE package -- skipping package."
  85. continue
  86. fi
  87. ## examine the list of installed items desribed in the bom
  88. BOM_FILE=$PACKAGE_PATH/$BOM_INTERMEDIATE_DIR/$LINE.bom
  89. lsbom $BOM_FILE > $TEMP_BOM_CONTENTS
  90. while read BOM_ITEM; do
  91. ## 3 column items describe just directories, 5 column items describe actual files
  92. COL_COUNT=$(echo $BOM_ITEM | awk '{print NF;}')
  93. if [ "$COL_COUNT" -eq 5 ]; then
  94. FILE_NAME=$DEFAULT_LOC/$(echo $BOM_ITEM | awk '{print $1;}')
  95. echo " . Removing $FILE_NAME"
  96. rm -rf $FILE_NAME
  97. fi
  98. done < $TEMP_BOM_CONTENTS
  99. ## remove package receipt
  100. echo " . Removing package receipt $PACKAGE_PATH"
  101. rm -rf $PACKAGE_PATH
  102. fi
  103. done < $PACKAGE_LIST_SRC
  104. ## nuke the user created by the install process.
  105. echo ". Removing created user $TOR_USER"
  106. niutil -destroy . /users/$TOR_USER
  107. ## clean up
  108. echo ". Cleaning up"
  109. rm -rf $TEMP_BOM_CONTENTS
  110. rm -rf /Library/Privoxy/ /Library/StartupItems/Tor/ /Library/StartupItems/Privoxy/ /Library/Tor/
  111. echo ". Finished"