cgi stuck in loop 
Author Message
 cgi stuck in loop

Hello all,
I'm modifying a previously written script and adding it to an interface for
an internal network monitoring scheme. I've seen to debugged to the point
where the script will run but it gets stuck caught in a loop, I can't seem
to find where I made the mistake. Warning the code is a bit long, (I've
commented out where the issue seems to be)   but if someone would mind
taking a look I'd appreciate it. For a visual reference, one can access the
script at www.techiekb.com/cgi-bin/test.pl

Thanks,
Drew

<START CODE>

#!/usr/bin/perl
#Make sure the above line is the path
#to perl on your server.
#################################################################
#   Add2File v1.0           Created 06/25/99                    #
#   By Zachary Jorgensen   http://www.*-*-*.com/           #
#   Feel free to mess around with this script.                  #
#   Just remember to hold this author harmless in the event of  #
#   any real or imaginary disasters arising from the use of     #
#   this script.  You may not sell this script.  You may        #
#   charge a reasonable fee for installing it but that's it.    #
#  _____          _     _       ____            _       _       #
# |__  /__ _  ___| |__ ( )___  / ___|  ___ _ __(_)_ __ | |_ ___ #
#   / // _` |/ __| '_ \|// __| \___ \ / __| '__| | '_ \| __/ __|#
#  / /| (_| | (__| | | | \__ \  ___) | (__| |  | | |_) | |_\__ \#
# /____\__,_|\___|_| |_| |___/ |____/ \___|_|  |_| .__/ \__|___/#
#                                                |_|            #
#################################################################

#######################################################################
#### Time for some minimal configuration.##############################
#######################################################################

#This is your password, it should be pretty secure tucked in here.
#Change zachrules to whatever you like.
$pass = "beta";

#Replace the path below with the absolute path to the file
#the script will be editing. It needs to be the absolute path
$datafile = "/home/drew/public_html/data.txt";

#Replace the path below with the absolute path to this script
$scriptlocation = "/cgi-bin/test.pl";

#Now create a folder in your cgi-bin called "add2file" and upload
#this script in ascII mode to that folder and set the privledges
#of this file to 755 read and execute privledges.

#Next chmod your data file 777 to give it read
#write and execute privledges.

#If you get an NTERNAL SERVER ERROR 500 then make sure that the path
#to your datafile is correct.
#######################################################################
#### Ok your done with the configuration part. You shouldn't have to  #
#### edit anything below here but if you want to, then go for it!     #
#######################################################################

#######################################################################
##########Get the input from the HTML form#############################
#This business here was from Dave Palmer###############################

#######################################################################

read(STDIN, $input, $ENV{'CONTENT_LENGTH'});

 # split the input

 # split the name/value pairs

 ($name, $value) = split(/=/, $pair);

 $name =~ tr/+/ /;
 $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 $value =~ tr/+/ /;
 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 $value =~ s/<([^>]|\n)*>//g;

  $FORM{$name} = $value;
    }
#######################################################################
##########Translate the data from the HTML form into scalar variables##
#######################################################################

$password = $FORM{'password'};
$zach = $FORM{'zach'};
$addthistofile = $FORM{'addthistofile'};

#######################################################################
########## Preform a security check####################################
#######################################################################

unless ($password eq "$pass") {

print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>Add2File</TITLE></HEAD>\n";
print "<BODY BGCOLOR=BLACK TEXT=WHITE><CENTER><H1>Update System
Status</H1><BR>\n";
print "<FORM ACTION=\"$scriptlocation\" METHOD=\"POST\">\n";
print "Please submit the admin password to use Add2File\.\n";
print "<BR><INPUT TYPE=TEXT SIZE=10 NAME=\"password\">\n";
print "<INPUT TYPE=SUBMIT VALUE=\"Submit your Password\">\n";
print "</FORM></CENTER></BODY></HTML>\n";
exit;

Quote:
}

&&&&&&&&& THIS IS THE PROBLEM BLOCK THAT KEEPS LOOPING
&&&&&&&&&&&&&&&&&&&&&&&&
#######################################################################
####Generate the configuration form page###############################
#######################################################################

unless ($zach eq "makechanges") {
print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title>Update System Status<\/title>\n";
print "<\/head>\n";
print "<body bgcolor=\"#666666\" marginwidth=\"0\" leftmargin=\"10\"
marginheight=\"10\" topmargin=\"0\" link=\"#ffffff\" vlink=\"#ffffff\"
alink=\"#ffffff\">\n";;
print "<center>\n";
print "<table width=\"590\" cellpadding=\"0\" cellspacing=\"0\" border=\"1\"
bgcolor=\"#666666\">\n";
print "<tr>\n";
print "<td valign=\"top\"><CENTER><b>Some image will go here</b>\n";
print "<\/td>\n";
print "<\/tr>\n";
print "<\/table>\n";
print "<table cellspacing\=0 cellpadding\=0 border\=0 width\=590>\n";
print "<tr>\n";
print "<td bgcolor=\"#666666\" colspan\=2><font face=\"arial\,helvetica\"
color=\"#ffffff\" size\=2><\/font><\/td>\n";
print "<\/tr>\n";
print "<\/table>\n";
print "<form method\=\"post\" action\=\"add2file.pl\">\n";
print "<center>\n";
print "<table cellspacing\=0 cellpadding\=0 border\=0>\n";
print "<tr>\n";
print "<td bgcolor=\"#000000\" colspan\=2><b><font face=\"verdana,arial\"
color=\"#ffffff\" size\=2>Update System Status<\/font><\/b><\/td>\n";
print "<\/tr>\n";
print "<tr>\n";
print "<th align\=left><font face=\"verdana,arial\" size=2
color=\"#ffffff\">Your Name<\/th>\n";
print "<td bgcolor=\"#e0e0e0\"><input name\=cid size\=50
value\=\"\"><\/td>\n";
print "<\/tr>\n";
print "<tr>\n";
print "<th align\=left><font face\=\"verdana,arial\" size\=2
color=\"#ffffff\">System<\/th>\n";
print "<td bgcolor\=\"#e0e0e0\"><select name\=\"product\"><option
value=\"11\" >Pacifier\n";
print "<option value\=\"1\" >Nwlink\n";
print "<option value\=\"2\" >Pro-Axis\n";
print "<option value\=\"3\" >Tst On Ramp\n";
print "<option value\=\"4\" >Hispeed\n";
print "<option value\=\"5\" >Kemba\n";
print "<option value\=\"6\" >Us.net\n";
print "<option value\=\"7\" >One.net\n";
print "<\/select><\/td>\n";
print "<\/tr>\n";
print "<tr>\n";
print "<th align\=left><font face\=\"verdana,arial\" size\=2
color=\"#ffffff\">Severity<\/th>\n";
print "<td bgcolor\=\"#e0e0e0\"><font face\=\"verdana,arial\"
size\=2><select name\=\"severity\">\n";
print "<option value\=\"0\">Critical\n";
print "<option value\=\"10\">Call\n";
print "<option value\=\"20\">Major\n";
print "<option value\=\"30\" SELECTED>Minor\n";
print "<\/select><\/td>\n";
print "<\/tr>\n";
print "<tr>\n";
print "<th align\=left><font face\=\"verdana,arial\" size\=2
color=\"#ffffff\">Date/Time<\/th>\n";
print "<td bgcolor=\"#e0e0e0\"><input name\=origin size\=50
value\=\"\"></td>\n";
print "<\/tr>\n";
print "<tr>\n";
print "<th align\=left><font face\=\"verdana,arial\" size\=2
color\=\"#ffffff\">Issue<\/th>\n";
print "<td bgcolor\=\"#e0e0e0\"><textarea name\=description cols\=60
rows\=10 wrap\=\"hard\"></textarea><\/td>\n";
print "<\/tr>\n";
print "<tr>\n";
print "<th align\=left valign\=top><font face\=\"verdana,arial\" size\=2
color\=\"#ffffff\">Tell Users<\/th>\n";
print "<td bgcolor\=\"#e0e0e0\"><textarea name\=description cols\=60
rows\=10 wrap\=\"hard\"></textarea><\/td>\n";
print "<\/tr>\n";
print "<tr>\n";
print "<th align\=left><font face\=\"verdana,arial\" size\=2
color\=\"#ffffff\">ETR<\/th>\n";
print "<td bgcolor=\"#e0e0e0\"><input name\=origin_email size\=50
value\=\"\"><\/td>\n";
print "<\/tr>\n";
print "<tr>\n";
print "<th align\=left><\/th>\n";
print "<td><br><input type\=\"submit\" value\=\"Update\"><input
type\=\"reset\" value\=\"Clear Form\"><\/td>\n";
print "<\/tr>\n";
print "<\/font>\n";
print "<\/table>\n";
print "<\/center>\n";
print "<\/form>\n";
print "<\/body>\n";
print "<\/html>\n";
exit;

Quote:
}

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&

#######################################################################
#### Write text to file ###############################################
#######################################################################

#open your file for writing, write to it then close file
die ("Cannot open your datafile\n") unless
open (FILE, ">>$datafile");
print FILE "$addthistofile\n";
close(FILE);

#######################################################################
#### Print Finish message #############################################
#######################################################################

print "Content-type: text/html\n\n";
 print "<HTML><HEAD><TITLE>Add2File</TITLE></HEAD>\n";
 print "<BODY BGCOLOR=Black TEXT=WHITE><CENTER><H1>Add2File</H1><BR>\n";
 print "Ok\, your file has been updated\.\n";
 print "Click here to see the update <a
href=\"http:\/\/www.techiekb.com\/~drew\/data.txt\">Updates<a\/>\n";
 print "</BODY></HTML>\n";
exit;

<END CODE>



Wed, 05 May 2004 15:38:08 GMT  
 cgi stuck in loop

Quote:

>the script will run but it gets stuck caught in a loop

I don't see a loop anywhere other than the cargo-culted URL
decoding code, so I don't think I can help you with the
problem you asked about, but your code leaves lots of room
for improvement.

Quote:
>if someone would mind
>taking a look I'd appreciate it.

It is too horrid to look upon, but I'm bored so I'll
endure the pain.

You really really ought to clean it up before you let anyone
else have access to it.

Quote:
>#!/usr/bin/perl

   #!/usr/bin/perl -w
   use strict;
   use CGI;
   use CGI::Carp qw(fatalsToBrowser);  # remove for production!
   $| = 1; # enable auto-flush

Take all the help you can get.

Quote:
>#This is your password, it should be pretty secure tucked in here.

                                      ^^^^^^^^^^^^^

That is plain not true.

Passwords in clear text cannot be called "secure".

Quote:
>#of this file to 755 read and execute privledges.

                  ^^^

You allow anyone else on the server to read the file containing
the password. That is not secure.

Quote:
>#Next chmod your data file 777 to give it read

                            ^^^

You allow anyone on the server to blow away the data file.

Quote:
>#This business here was from Dave Palmer###############################

>read(STDIN, $input, $ENV{'CONTENT_LENGTH'});

[snip URL decoding code]

The CGI module will do this for you. More importantly, it will
do it _correctly_.

Quote:
>$password = $FORM{'password'};
>$zach = $FORM{'zach'};
>$addthistofile = $FORM{'addthistofile'};

You do not need to copy them, you can use them straight from
the %FORM hash.

Quote:
>#######################################################################
>########## Preform a security check####################################
>#######################################################################

>unless ($password eq "$pass") {

                      ^     ^
                      ^     ^

Those quotes perform no useful function, so they should not be there.

I think it is easier to understand written this way:

   if ( $password ne $pass ) {

Quote:
>print "Content-type: text/html\n\n";
>print "<HTML><HEAD><TITLE>Add2File</TITLE></HEAD>\n";
>print "<BODY BGCOLOR=BLACK TEXT=WHITE><CENTER><H1>Update System
>Status</H1><BR>\n";
>print "<FORM ACTION=\"$scriptlocation\" METHOD=\"POST\">\n";
>print "Please submit the admin password to use Add2File\.\n";

                                                        ^
                                                        ^

That backslash performs no useful function, so it should not be there.

Quote:
>print "<BR><INPUT TYPE=TEXT SIZE=10 NAME=\"password\">\n";
>print "<INPUT TYPE=SUBMIT VALUE=\"Submit your Password\">\n";
>print "</FORM></CENTER></BODY></HTML>\n";

I think it is easier to understand written this way:

print <<ENDHTML;
Content-type: text/html

<HTML><HEAD><TITLE>Add2File</TITLE></HEAD>
<BODY BGCOLOR=BLACK TEXT=WHITE><CENTER><H1>Update System Status</H1><BR>
<FORM ACTION="$scriptlocation" METHOD="POST">
Please submit the admin password to use Add2File.
<BR><INPUT TYPE=TEXT SIZE=10 NAME="password">
<INPUT TYPE=SUBMIT VALUE="Submit your Password">
</FORM></CENTER></BODY></HTML>
ENDHTML

Look Ma! No backslashes!

Read about "here-document" in perldata.pod

Quote:
>&&&&&&&&& THIS IS THE PROBLEM BLOCK THAT KEEPS LOOPING

                                          ^^^^^^^^^^^^^

That is not possible, as there is no loop here. I dunno what's going on...

Quote:
>unless ($zach eq "makechanges") {

   if ( $zach ne 'makechanges' ) {

[snip about *80* individual print() statements]

If you want folks to spend their time helping you, then you need
to enable warnings and strictures, and use the CGI module, else
you are pretty much on your own. Good luck though.

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Fri, 07 May 2004 00:28:24 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. corruption causes (technical explanations)

2. Array stuck within a loop

3. Newbie: stuck in endless loop!

4. CGI.pm forms not "sticking"

5. cgi newbie stuck...

6. DBGrid

7. The Problem w/ String

8. Loop generation of HTML tables with CGI.pm

9. CGI / foreach loop question

10. Perl, CGI, ServerPush, & an almost infinite loop

11. Why does using index() outside loop slow down loop

12. next to -n loop in nested loops

 

 
Powered by phpBB® Forum Software