Fighting root

Sunday, December 3rd, 2006 - Español English

The sysadmin at my former employer had the bad habit of uploading files as root to directories we all used. It was a real PITA, so I hacked this script to fix the perms, without having to be root. No magic: the directory has to be writable.

  1. #!/bin/bash
  2. #
  3. # Wed Nov 23 19:25:14 CET 2005 * <nonick AT 8027 DOT org>
  4. #
  5. # Aarggh....
  6. #
  7.  
  8. ##################
  9. # Load lib
  10. #
  11. if [ ! -f ~/bin/messages.sh ]; then
  12. echo "~/bin/messages.sh is required but not found. Bye..."
  13. exit 1
  14. fi
  15. . ~/bin/messages.sh
  16.  
  17.  
  18. ##################
  19. # Check params
  20. #
  21. if (( ${#@} < 1 ))
  22. then
  23. echo
  24. echo $(basename $0)
  25. echo
  26. echo " Looks for files owned by root and moves and copies them to regain ownership"
  27. echo " This needs the target directory to be writable by the user"
  28. echo
  29. echo " Usage:"
  30. echo " $0 <directory>"
  31. echo
  32. exit 1
  33. fi
  34.  
  35. dir=$1
  36.  
  37. if [ ! -d "$dir" ] || [ ! -w "$dir" ]
  38. then
  39. error_msg "Error: either '$dir' is not a directory or it is not writable."
  40. exit 2
  41. fi
  42.  
  43. ##################
  44. # Go
  45. #
  46. info_msg "\nRegaining ownership of files in directory '${BLUE}$dir${OFF}'...\n\n"
  47.  
  48. for file in $(find $dir -name '*php')
  49. do
  50. group=$(stat -c %g $file)
  51. cnt=0
  52. if (( group == 0 ))
  53. then
  54. run_cmd "File ${RED}${file}${OFF} belongs to root, moving ... "\
  55. "mv $file $file~~ && cp $file~~ $file"
  56. let cnt=$cnt+1
  57. fi
  58. done
  59.  
  60. info_msg "\n${BLUE}${cnt}${OFF} files needed moving.\n\n"
  61.  
  62. if (( cnt > 0 ))
  63. then
  64. if ask_msg "I left backup files. Remove them? " "N"
  65. then
  66. #run_cmd "Removing ... " "find $dir -iname '*php~~' -exec rm \{\} \;"
  67. info_msg "${RED}Feature disabled${OFF}\n\n"
  68. fi
  69. fi
  70.  
  71. exit 0;

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>