School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,489 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,818 people online right now. Registration is fast and FREE... Join Now!




File Permissions - Perl

 

File Permissions - Perl, Last step!

n00bc0der

5 Jun, 2009 - 06:01 AM
Post #1

D.I.C Head
**

Joined: 27 Nov, 2008
Posts: 62



Thanked: 3 times
My Contributions
Hey guys,

There have been many people that have helped me in the loss of my perl virginity over the past two weeks. I've finally come up with a product that is 90% complete. All I have left to do is get file permissions for each of the folders and files that are found in these long directory/file lists. I'm completely stumped on how to do this. I've looked through some co-workers unix text books and googled some stuff but I just can't get anything to work the way I want it to work.

So what I want to do is simply write a subroutine that I can call foreach element in my array of files and folders, that will get the permissions for that file. I want to know who can do what with it.

Please note I can't use modules, and I've made it this far not using any. Without further ado, here's the code thus far:

CODE
#!/usr/bin/perl

###############################################################
#This script is used to ensure the permissions of significant
#files are set correctly, and that the correct number of files
#and directories are existent in the proper location.
###############################################################

#Used to detect all directories in a given original directory.
sub getfulldirectorylist {
    my @final = ();
    my $dirtop = @_[0];
    print "Reading data from $dirtop\n";
    my @dirlist = `find $dirtop -type d`;
    my $dirlist = @dirlist;
    for ($i = 0; $i < $dirlist; $i++) {
        my $temp = @dirlist[$i];
        $temp =~ s/( |\$)/\\$1/g;
        if ($temp =~ m/\/store\/ariel/) {
        }
        else {
        @final[$i] = $temp;
        }
    }
    return @final;
}


#Used to detect all files found in a given directory.
sub getdirectorycontents {
    my $dirname = @_[0];
    my @contents = `ls -A $dirname`;
    return @contents;
}

#Used to detect the permissions of a given file.
sub getpermissions {
    my ($filename, $location) = @_;

    my $permission = `cd $location; ls -l | grep $filename | grep nobody`;
    if ($permission) {
            return "nobody";
    }

    my $permission = `cd $location; ls -l | grep $filename | grep root`;
    if ($permission) {
        return "root";
    }
}

#Compares the baseline file with the file being tested.
sub compare {
    my ($baseline, $test) = @_;
    open(FILE, $baseline) or die "Can't open $baseline : $!";
    my @baselinefile = <FILE>;
    close(FILE);
    open(TEST, $test) or die "Can't open $test : $!";
    my @testfile = <TEST>;
    close(TEST);
    my $baselinefile = @baselinefile;
    my $testfile = @testfile;
    print "\nStarting matching process\n";
    my @testresults = ();
    $found = 0;
    $unfound = 0;
    my @unfoundlist = ();
    for ($i = 0; $i < $baselinefile; $i++) {
        if (($i % 10000) == 0) {
            print ".";
        }
        for ($j = 0; $j < $testfile; $j++) {
            if (@baselinefile[$i] eq @testfile[$j]) {
                $found = 1;
                last;
            }
        }
        if ($found == 0) {
            push(@unfoundlist, "@baselinefile[$i]");
            $unfound++;
        }
        $found = 0;
    }
    $result = $baselinefile - $unfound;
    print "\n$result out of $baselinefile files & folders were sucessfully identified\n\n";
    print "Writing unfound results to text file: results.txt\n\n";
    print "Comparison complete.\n";
    
    write_file("results.txt", @unfoundlist);
}

#Used for writing the file to be tested.
sub write_file {
    my ($f, @data ) = @_;
    @data = () unless @data;
    open F, "> $f" or die "Can't open $f : $!";
    print F @data;
    close F;
}

#Will populate a file with a list all the directories and files
sub get_info {
    my $filename = @_[0];
    my @list = ();
    print "\n";
    for my $topdir ("/store", "/opt/qradar") {
        my @dir = getfulldirectorylist("$topdir");
        foreach $dir (@dir) {
            my @content = getdirectorycontents($dir);
            push(@list, "$dir");
            foreach $content (@content) {
                push(@list, "$content");
            }
            push(@list, "\n");
        }
    }
    write_file("$filename", @list);
}

##############################################################
#Usability for simple creation of a baseline file or test file
##############################################################

print "What would you like to do?\n1. Create a baseline - type '1'.\n2. Test a box - type '2'.\n";
$input = <STDIN>;
chomp $input;

if ("$input" eq "1") {
    get_info("baseline.txt");
    print "Baseline written. See baseline.txt for results\n";
}

elsif ("$input" eq "2") {
    get_info("test.txt");
    compare("baseline.txt", "test.txt");
}


Thanks Guys!!

Edit *** Ignore my getpermissions sub routine. It needs to be replaced!

This post has been edited by n00bc0der: 5 Jun, 2009 - 06:02 AM

User is offlineProfile CardPM
+Quote Post


KevinADC

RE: File Permissions - Perl

5 Jun, 2009 - 01:34 PM
Post #2

D.I.C Regular
Group Icon

Joined: 23 Jan, 2007
Posts: 401



Thanked: 25 times
Dream Kudos: 50
My Contributions
All your perl script is really doing is being used as a wrapper to run OS commands. So why not use the OS command that returns the folder/file permissions? If you want to use perl look into the stat() function. The third element returned from stat is the mode.
User is offlineProfile CardPM
+Quote Post

mcwolf

RE: File Permissions - Perl

8 Jun, 2009 - 04:22 AM
Post #3

New D.I.C Head
*

Joined: 26 Apr, 2009
Posts: 13


My Contributions
QUOTE(n00bc0der @ 5 Jun, 2009 - 06:01 AM) *

Hey guys,

There have been many people that have helped me in the loss of my perl virginity over the past two weeks. I've finally come up with a product that is 90% complete. All I have left to do is get file permissions for each of the folders and files that are found in these long directory/file lists. I'm completely stumped on how to do this. I've looked through some co-workers unix text books and googled some stuff but I just can't get anything to work the way I want it to work.

So what I want to do is simply write a subroutine that I can call foreach element in my array of files and folders, that will get the permissions for that file. I want to know who can do what with it.

Please note I can't use modules, and I've made it this far not using any. Without further ado, here's the code thus far:

CODE
#!/usr/bin/perl

###############################################################
#This script is used to ensure the permissions of significant
#files are set correctly, and that the correct number of files
#and directories are existent in the proper location.
###############################################################

#Used to detect all directories in a given original directory.
sub getfulldirectorylist {
    my @final = ();
    my $dirtop = @_[0];
    print "Reading data from $dirtop\n";
    my @dirlist = `find $dirtop -type d`;
    my $dirlist = @dirlist;
    for ($i = 0; $i < $dirlist; $i++) {
        my $temp = @dirlist[$i];
        $temp =~ s/( |\$)/\\$1/g;
        if ($temp =~ m/\/store\/ariel/) {
        }
        else {
        @final[$i] = $temp;
        }
    }
    return @final;
}


#Used to detect all files found in a given directory.
sub getdirectorycontents {
    my $dirname = @_[0];
    my @contents = `ls -A $dirname`;
    return @contents;
}

#Used to detect the permissions of a given file.
sub getpermissions {
    my ($filename, $location) = @_;

    my $permission = `cd $location; ls -l | grep $filename | grep nobody`;
    if ($permission) {
            return "nobody";
    }

    my $permission = `cd $location; ls -l | grep $filename | grep root`;
    if ($permission) {
        return "root";
    }
}

#Compares the baseline file with the file being tested.
sub compare {
    my ($baseline, $test) = @_;
    open(FILE, $baseline) or die "Can't open $baseline : $!";
    my @baselinefile = <FILE>;
    close(FILE);
    open(TEST, $test) or die "Can't open $test : $!";
    my @testfile = <TEST>;
    close(TEST);
    my $baselinefile = @baselinefile;
    my $testfile = @testfile;
    print "\nStarting matching process\n";
    my @testresults = ();
    $found = 0;
    $unfound = 0;
    my @unfoundlist = ();
    for ($i = 0; $i < $baselinefile; $i++) {
        if (($i % 10000) == 0) {
            print ".";
        }
        for ($j = 0; $j < $testfile; $j++) {
            if (@baselinefile[$i] eq @testfile[$j]) {
                $found = 1;
                last;
            }
        }
        if ($found == 0) {
            push(@unfoundlist, "@baselinefile[$i]");
            $unfound++;
        }
        $found = 0;
    }
    $result = $baselinefile - $unfound;
    print "\n$result out of $baselinefile files & folders were sucessfully identified\n\n";
    print "Writing unfound results to text file: results.txt\n\n";
    print "Comparison complete.\n";
    
    write_file("results.txt", @unfoundlist);
}

#Used for writing the file to be tested.
sub write_file {
    my ($f, @data ) = @_;
    @data = () unless @data;
    open F, "> $f" or die "Can't open $f : $!";
    print F @data;
    close F;
}

#Will populate a file with a list all the directories and files
sub get_info {
    my $filename = @_[0];
    my @list = ();
    print "\n";
    for my $topdir ("/store", "/opt/qradar") {
        my @dir = getfulldirectorylist("$topdir");
        foreach $dir (@dir) {
            my @content = getdirectorycontents($dir);
            push(@list, "$dir");
            foreach $content (@content) {
                push(@list, "$content");
            }
            push(@list, "\n");
        }
    }
    write_file("$filename", @list);
}

##############################################################
#Usability for simple creation of a baseline file or test file
##############################################################

print "What would you like to do?\n1. Create a baseline - type '1'.\n2. Test a box - type '2'.\n";
$input = <STDIN>;
chomp $input;

if ("$input" eq "1") {
    get_info("baseline.txt");
    print "Baseline written. See baseline.txt for results\n";
}

elsif ("$input" eq "2") {
    get_info("test.txt");
    compare("baseline.txt", "test.txt");
}


Thanks Guys!!

Edit *** Ignore my getpermissions sub routine. It needs to be replaced!



Along with that stat(), you could try something like this.....

CODE
#Get info on the file
@statinfo = stat("$dirname/$name");
# Is it a directory
if (-d(_))
{
     #Yes, print the name, size and date
     print ("$name (directory), last modified " .
scalar(localtime($statinfo[9])) . "\n");
     }
   else
     {
     print ("$name, size = " . $statinfo[7] . ", last modified " .
scalar(localtime($statinfo[9])) . "\n");
     }
     }





User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 04:16AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month