0

I am trying to locate any user home directories and their files that are writable by anyone.

I wrote this:

find /home -type d -perm -200 | grep . || 
   echo no world-writable directories here

This is only detecting directories, but I would like to also add files.

2 Answers 2

0
getent passwd | 
cut -d: -f1,6 | 
while IFS=: read user home; do 
    if [[ -d $home ]] && (( (0$(stat -c %a $home) & 2) == 2 )); then 
        echo $user $home
    fi
done

The test for world-writable can also be written like this:

[[ $(stat -c %A $home) == *?w? ]]

Explained:

  1. getent passwd - get information about all users on the system in an /etc/passwd format
  2. cut ... - select only userid and home directory
  3. [[ -d $home ]] - ensure home-dir is a directory
  4. (( (0$(stat -c %a $home) & 2) == 2 )):
    stat -c %a $home returns the file permissions in octal form (ex: 755). Prefix a zero so that bash knows it's an octal number. Then perform a bitwise-and to get the "world-writable" bit. If that value is zero, then the directory is not world writable. If the value is 2 (binary 10), then the directory is world writable.
  5. So, if it is a directory, and it is world-writable, then output the userid and directory.
  6. the alternate form [[ $(stat -c %A $home) == *?w? ]] looks at the file permissions in string form (ex: drwxr-xr-x) and then checks to see if the 2nd last character is a "w"
2
  • I don't understand some lines, what is happening at if:
    – user230654
    Jun 12, 2013 at 14:07
  • @user230654, explanation added Jun 12, 2013 at 16:22
0

I'm not sure what you're trying to do. If you want to find all files and directories under /home that have permissions of 200, you can do

find /home -perm -200

That, however, is not the list of world writeable files and folders, 200 means the file is writeable by its owner. If you want all files and folders that can be written by everyone do this instead:

find /home -perm -a=w

Your grep . does nothing. . means "Match ANY character" so it will always be true. If you are looking for files or folders whose name begins with ., you can do need grep "^\.".

1
  • find . -perm -002 this is my final :) thanx
    – user230654
    Jun 12, 2013 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .