Ok, I'm trying to write a perl cgi program that lets the user input a file that they want to open and what they want to search for. So for example, they will go to a site and have two fields in a table. One to specify the file name and the other to enter their search criteria. I'm having a hard time actually figuring out how to implement.
Ok, so I'm thinking I can use the find function to do this. What I would like to do it let the user imput the files name. I can have perl do a find and locate the file. I want to be able to store the path as a string and use it to open the file. Any suggestions on how to make this work?
Last edited by WingedPanther; 12-17-2009 at 01:46 PM. Reason: Double post
Learn Perl. If you knew the language, you'd know how simple this was.
Seriously though, if you use <STDIN> and open(FILE, $filevar), you pretty much have it.
One thing: Make sure people can't input "../" because then they can access documents you don't want them reaching. Make one folder for the files people are *allowed* to access, and then forbid "../" in the string. Otherwise, you have a path traversal exploit.
Lookie here: The Web Application Security Consortium / Path Traversal
I actually just did this, its my 2nd perl script ever, its pretty easy man... You just need to do some googling.
Just to be nice, ill provide my code
Code:#!c:/perl/bin
use File::Copy;
use Fatal qw(copy);
&start;
# BEGIN, CHOOSING THE MAIL SERVER
sub start{
system("cls");
print "What mail server?> ";
chomp($svr = <>);
print "\n";
&CHOOSE;
}
# CHOOSE THE PROTOCOL
sub CHOOSE {
system("cls");
print "What type of logs for $svr?\n";
print "1 - SMTP\n";
print "2 - POP\n";
print "3 - Delivery\n";
print "4 - IMAP\n";
print "> ";
chomp($input = <>);
if($input == 1){
$type = "smtpLog";
} elsif($input == 2){
$type = "popRetrieval";
} elsif($input == 3){
$type = "delivery";
} elsif($input == 4){
$type = "imapLog";
} else {
print "Please type 1 2 3 or 4\a\n";
sleep(2);
&CHOOSE;
}
&date;
}
# CHOOSE THE DATE
sub date {
system("cls");
print "What date? (EG: YYYY.DD.MM)> ";
chomp($date = <>);
# REGEX THE DATE
if( $date =~ /[^d{4}.d{2}.d{2}]/){
print "Bad Date Specified\n";
&DATE;
}
&crap;
}
sub crap {
# SEARCH STRING
system("cls");
print "String to search> ";
chomp($search = <>);
# PUT LOG PATH TOGETHER
print $date . "-" . $type . ".log\n";
my $remote_file = "\\\\".$svr."\\e\$\\SmarterMail\\Logs\\".$date."-".$type.".log";
# CHECK IF LOG EXISTS
if(! -e $remote_file ) {
print "Can't find ".$remote_file." (Maybe too far back?)\n";
exit;
}
# OPEN THE LOG FILE
open (FILE, $remote_file);
# CREATE THE RESULTS FILE
open (RESULTS, ">results.txt");
# RESULTS FILE OPENING TEXT
print RESULTS "Search for " .$search. " in " .$remote_file. "\n";
print "Opened ".$remote_file." for reading..\n";
# set blank session _search, so it can be used later
$session_search = "";
$count = 0;
$tmp_count = 0;
while (<FILE>) {
chomp;
if($type eq "delivery"){ # DELIVERY LOG CHOMP
($date, $session, $rest) = split(" ");
} elsif($type eq "smtpLog") { # SMTP LOG CHOMP
($date, $ip, $session, $rest) = split(" ");
} elsif($type eq "popRetrieval") { # POP LOG CHOMP
($date, $ip, $session, $rest) = split(" ");
} elsif($type eq "imapLog") { # IMAP LOG CHOMP
($date, $ip, $session, $rest) = split(" ");
} else { # NULL CHOMP
print "Unable to continue, no log chomp available\n";
sleep(2);
exit;
}
$line = $_;
if (($line =~ m/$search/i) || ($session eq $session_search)) {
if($session_search eq $session){
print RESULTS $line. "\n";
} else {
$session_search = $session;
print RESULTS $line. "\n";
}
}
# KEEP LOG COUNT AND UPDATE TECH
$count++;
$tmp_count++;
if($tmp_count == 5000){
print "Searched " .$count. " lines..\n";
$tmp_count = 0;
}
}
close (RESULTS);
close (FILE);
print "\nSearch Complete!\n";
# DONE!
exec 'results.txt';
# THE END
}
print "The End\n";
Checkout my new forum! http://adminreference.com/
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks