This script walks the Linux directory structures to files, while checking ACL access to them and displaying the access rights to all subdirectories.

It can tell you if another user is able to access your files or not, or when used with sudo, check for access of files not owned by you, for any other user on the system. Native permissions (chmod) are shown in uppercase to distinguish them from ACL permissions (setfacl), which are lowercase.

Let's start with a Usage Example. I naively give user nobody access to t.pl, but as $HOME directories are closed and secure, the actual access is prevented:

fbrm@debian:~$ setfacl -m u:nobody:r ~/CODE/PERL/t.pl fbrm@debian:~$ find $HOME/CODE/PERL -name '*.pl' | USR=nobody ./showpe +rms.pl | column -t DEBUG: Testing permissions against user 'nobody' / OTHER::r-x /home/ OTHER::r-x /home/fbrm/ --- /home/fbrm/CODE/ OTHER::r-x /home/fbrm/CODE/PERL/ OTHER::r-x /home/fbrm/CODE/PERL/showperms.pl OTHER::r-x /home/fbrm/CODE/PERL/t.pl user:nobody:r-- fbrm@debian:~$ find $HOME/CODE/PERL -name '*.pl' | ./showperms.pl | co +lumn -t DEBUG: Testing permissions against user 'fbrm' / OTHER::r-x /home/ OTHER::r-x /home/fbrm/ USER:fbrm:rwx /home/fbrm/CODE/ USER:fbrm:rwx GROUP:fbrm:r-x /home/fbrm/CODE/PERL/ USER:fbrm:rwx GROUP:fbrm:rwx /home/fbrm/CODE/PERL/showperms.pl USER:fbrm:rwx GROUP:fbrm:rwx /home/fbrm/CODE/PERL/t.pl USER:fbrm:rw- GROUP:fbrm:rw-

TIP: Instead of a find, you can also cat a file with fully qualified files, or echo a file.

#!/usr/bin/perl use strict; use warnings; my $DEBUG = 1; my $u = $ENV{USR} || $ENV{USER}; # Set default user to check permissio +ns for warn "DEBUG: Testing permissions against user '$u'\n" if $DEBUG; my $g; my $o; my %P; my $file; while( $file = <STDIN> ) { chomp($file); if ($file && -e $file) { my @F = split /\//, $file; while(@F){ $P{ join("/", @F) || "/" } = 0; # Build a list of Fully Qu +alified paths. Deduplicated. pop @F; $F[-1] .= "/" if @F; # also add the root path to check }; }else{ warn "Could not find file '$file' (maybe run this as root to g +et access to the file?)\n"; } } for my $k (sort keys %P) { my @A = `getfacl "$k" 2>/dev/null | grep -v -e default: -e file:`; + # grab output ($o) = map { /# owner: (\S+)/ } @A; # Get the file owner ($g) = map { /# group: (\S+)/ } @A; # Get the file group grep { s/user::/USER:$o:/} @A; # make native Linux dir/file permis +sions uppercase grep { s/group::/GROUP:$g:/} @A; # idem grep { s/other::/OTHER::/} @A; # idem $P{$k} = join "\n", grep { !/^#/ && /$u|^other/i } @A; $P{$k} =~ s/:[^:]+effective:/:/; # consider only effective permiss +ions $P{$k} =~ s/\S+:---//g; #remove empty permissions $P{$k} =~ s/(?:user|group):[\s\S]*\K(other:.*)//mi; $P{$k} =~ s/[\n\r\s]+/ /g; # remove newlines $P{$k} = '---' if $P{$k} eq " "; # if no permissions, default to - +-- }; for my $k (sort keys %P){ print "$k $P{$k}\n"; }

In reply to Linux Show Permissions: traverse file access by FreeBeerReekingMonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.