[comp.unix.questions] root owned files/world writable

epperly@cs.odu.edu (William "Badger" Epperly) (11/15/90)

Hey, folks!  If this is the wrong place for this article
you have my apologies, but where else would it go?

Listen, I have a problem here.  I am trying to write 
a small shell script that will search a network, made up of nine
servers, for all files owned by root that have world writeable
permissions.  I have tried working with the find command and its
-perm option but it does not seem to take wild cards (although the
man page shows an example using * and ?, but these only appear to
be for filenames), so that is unusable by itself.  I have also tried
a combination of find and awk.  The trouble here is that the man
page for awk is so poorly written that it is hard to tell exactly
what you can do with the different options and constructs.  This is
what I tried

find / -user root -ls | awk {'if substr($1,9,1)=w print>test.out

granted this doesn't work, so no flames about my ignorance please.
Any help would be appreciated though, and you would have my undying
gratitude, which along with $.75 will get you a cup of coffee at
7-11.
anyway, thanks in advance.
Please send replies to 
	epperly@cd.odu.edu

			William Epperly 

weimer@ssd.kodak.com (Gary Weimer) (11/16/90)

In article <1990Nov14.224701.15657@cs.odu.edu> epperly@cs.odu.edu (William "Badger" Epperly) writes:
>
[trying to find world-writeable files owned by root. following doesn't work]
>
>find / -user root -ls | awk {'if substr($1,9,1)=w print>test.out

first, syntax for awk is wrong, you want something like:

    awk ' {if (substr($1,9,1) == "w" ) print $8 > "test.out" } '
        ^ ^   ^               ^^ ^ ^ ^       ^^   ^        ^ ^ ^
        1 2   3                4 5 5 3       6    5        5 2 1

1) a single quote needs to surround the <format> of awk
2) currly brackets are also needed for <format>
3) if stmt's condition needs to be surrounded by parens (sp?)
4) equallity is tested using double =
5) all text must be enclosed in quotes, or it is assumed to be a variable
6) you may or may not want to add this to get only the file name

second, find's -ls does not produce the same output as 'ls -al'. It
produces something like:

 9601    1 drwxr-xr-x  3 weimer   staff         512 Nov 13 11:42 file_name

so the command you want to use would be:

find / -user root -ls | awk '{if (substr($3,9,1)=="w") print $11 > "test.out"}'

Hope this helps.

P.S. you might also want to find file owned by root that are group writeable
and are part of some universal group (like 'user').

Gary Weimer

hunt@dg-rtp.rtp.dg.com (Greg Hunt) (11/17/90)

In article <1990Nov14.224701.15657@cs.odu.edu>, epperly@cs.odu.edu (William "Badger" Epperly) writes:
> 
> Hey, folks!  If this is the wrong place for this article
> you have my apologies, but where else would it go?
> 

This is the right place.  Ask away!

>
> Listen, I have a problem here.  I am trying to write 
> a small shell script that will search a network, made up of nine
> servers, for all files owned by root that have world writeable
> permissions.  I have tried working with the find command and its
> -perm option but it does not seem to take wild cards (although the
> man page shows an example using * and ?, but these only appear to
> be for filenames), so that is unusable by itself.

It isn't easy (unfortunately) to figure out how to do more complex
things like this with find.  My find allows me to do this:

    find / \( -user root -perm -2 \) -print

to find files owned by root that also have world write permission.

To get find to do the "and" of two conditions, you need to put them
next to each other and within a set of parentheses.  Since  ( and )
have special meaning to the shell, they have to be escaped, so you
put a \ in front of them.

Putting a - in front of the permission value says "only look at the
bits I specify instead of looking at the whole permission value".
So the '-2' makes it look for files with the world write bit turned
on, and makes it ignore all the other permission bits regardless of
whether they are on or off.

Give this a try on your system.  There's no guarantee, however, that
your version of find supports doing things this way.

Enjoy!

--
Greg Hunt                        Internet: hunt@dg-rtp.rtp.dg.com
DG/UX Kernel Development         UUCP:     {world}!mcnc!rti!dg-rtp!hunt
Data General Corporation
Research Triangle Park, NC       These opinions are mine, not DG's.

Dan_Jacobson@ATT.COM (11/18/90)

>>>>> On 16 Nov 90 18:21:50 GMT, hunt@dg-rtp.rtp.dg.com (Greg Hunt) said:

Greg>     find / \( -user root -perm -2 \) -print

I don't want to be dim, but isn't this the same as

          find / -user root -perm -2 -print

[both work ok]
-- 
Dan_Jacobson@ATT.COM  Naperville IL USA  +1 708-979-6364

hunt@dg-rtp.rtp.dg.com (Greg Hunt) (11/26/90)

In article <DANJ1.90Nov17154440@cbnewse.ATT.COM>, Dan_Jacobson@ATT.COM writes:
> >>>>> On 16 Nov 90 18:21:50 GMT, hunt@dg-rtp.rtp.dg.com (Greg Hunt) said:
> 
> Greg>     find / \( -user root -perm -2 \) -print
> 
> I don't want to be dim, but isn't this the same as
> 
>           find / -user root -perm -2 -print
> 
> [both work ok]

Yup.  My mistake.  Thanks for pointing it out.

What I was thinking is that when you have to put one of the special
symbols between the conditions (like -o for "or"), then you have to
use the \( and \).  Since there is no special symbol for "and", just
putting the conditions next to each other, you don't have to use the
parentheses.

--
Greg Hunt                        Internet: hunt@dg-rtp.rtp.dg.com
DG/UX Kernel Development         UUCP:     {world}!mcnc!rti!dg-rtp!hunt
Data General Corporation
Research Triangle Park, NC       These opinions are mine, not DG's.