Search code examples
bashgreprhel5

BASH - Find all world writable files referenced in a set of files


Question: how can I find all world-writable files that are referenced in the startup scripts found in the /etc/init.d directory of a RHEL 5 server?

Constraints:

  • BASH solutions only (this is part of a larger script)
  • No additional tools/packages can be installed

Here's what I have so far.

worldWritable=$(find / -type f -perm -002)
startupScripts=$(ls -l /etc/init.d/* | tr '\011' ' ' | tr -s ' ' | cut -f 9,9 -d " ")
for file in $worldWritable
do
    for line in $startupScripts
    do
        grep -w "$file" $line | grep -v "^#" >> outFile
    done
done

This sort of works, but it takes a LONG time, and it includes a lot that's not correct (at least not what I'm looking for). I just need "outFile" to contain a list of the world-writable files found to be referenced in any script in the /etc/init.d directory.

I don't mind completely abandoning this approach if anyone can offer a better solution. I just need something faster and more reliable. Thanks!


Solution

  • The major slowness certainly comes from the find /, scanning the entire filesystem. It will be faster to do the converse:

    • Extract all the absolute paths from the init scripts
      • Using an appropriate regex, for example excluding matches where a # occurs earlier on the same line
    • For each extracted potential path, check that:
      • The file actually exists
      • The file is world writable

    The result should be significantly faster.