Site hosted by Angelfire.com: Build your free website today!

Common Gateway Interfacing Using Perl

The follow are sample perl scripts are available for your disposal, feel free to manipulate and distribute copies at your own risk. These are lacking in source documentation and are very messy, but usefull in establishing your own personal apache or any other http server with cgi support.


oh, did i mention this was VERY messy ? if you good at wot you do im sure you'll work it out

newuser.cgi

#!/usr/bin/perl print "Content-type:text/html\n\n"; $remotehost = "192.168.0.1"; read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } if ($FORM{username} eq '' or $FORM{password} eq '') { &Failed; } #********************************************************************** open(INF,"access.log") or dienice("Failed To Open Access $!"); @access = <INF>; close (INF); foreach $line (@access) { $usrval = "$FORM{username}:"; if($line =~ /$usrval/) { &UsedName; die; } } &Success; #********************************************************************** sub Success { print "<html><head><title>Registration</title></head><body>"; print "<body bgcolor=\"black\" text=\"green\" alink=\"black\" vlink=\"black\"> <center>"; print "<h2>You Have Been Successfully Registered</h2>\n"; print "<h2>Enjoy Yourself </h2>"; print "</center>"; print "Your Loggon Details Are : <br><br>"; print "User Name: $FORM{username} <br>"; print "User Password: $FORM{password} <br> <br><br>"; print "<a href=\"http://$remotehost/logon.html\"> <img src=\"http://192.168.0.1/signin.jpg\"> </a><br> <br><br><br> "; print "<img src=\"http://$remotehost/scrubnet.jpg\" height=\"200\" width=\"600\"> "; #Write Access Files open(OUTF,">>access.log") or dienice("Error: Cannot Open Access Log : $!"); flock(OUTF,2); seek(OUTF,0,2); print OUTF "$FORM{username}:$FORM{password}; \n"; print "</body></html>"; close(OUTF); } sub Failed { print "<html><head><title>Registration</title></head><body>"; print "<body bgcolor=\"black\" text=\"green\"> <center>"; print "<h2>Your Registration Has Failed</h2>\n"; print "<p>Please Enter A Valid Username Or Password </p> <br><br><br>"; print "<br><br><img src=\"http://$remotehost/naughty.jpg\" height=\"150\" width=\"600\"> "; print "</center> </body></html>"; die; } sub UsedName { print "<html><head><title>Registration</title></head><body>"; print "<body bgcolor=\"black\" text=\"green\"> <center>"; print "<h2>Sorry The Username Already Exist's Try Another </h2>"; print "</center> </body></html>"; }

Logon.cgi

#!/usr/bin/perl print "Content-type:text/html\n\n"; $remotehost = "192.168.0.1"; read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } open(INF,"access.log") or dienice("Failed To Open Access $!"); @access = <INF>; close (INF); foreach $line (@access) { $strval = "$FORM{username}:$FORM{password};"; if($line =~ /$strval/) { &AccessGranted; } }

msgbrd.cgi

#!/usr/bin/perl print "Content-type:text/html\n\n"; print "<html> <head> <title> Posted Messages </title> </head>"; print "<body bgcolor=\"black\" text=\"green\">"; open(INF,"forum.log") or dienice("Failed To Open Forum $!"); @access = <INF>; close (INF); foreach $line (@access) { print "$line"; }

sendmsg.cgi

#!/usr/bin/perl print "Content-type:text/html\n\n"; #$remotehost = "203.220.193.25"; read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } #********************************************************************** open(INF,"remoteaccess.log") or dienice("Failed To Open Remote Access $!"); @access = <INF>; close (INF); $usrval = "$ENV{'REMOTE_ADDR'}:"; foreach $line (@access) { if($line =~ /$usrval/) { &Success; die; } } &AccessDenied; sub Success { print "<html><head><title>Message Sent</title></head> <body bgcolor=\"black\" text=\"green\">"; print "<center>"; print "<h2>Your Message Has Been Successfully Posted</h2> </center> </body> </html>"; open(OUTF,">>forum.log") or dienice("Error: Cannot Open Forum Log : $!"); flock(OUTF,2); seek(OUTF,0,2); #********************************* open(INF,"remoteaccess.log") or dienice("Failed To Open Remote Access $!"); @access = <INF>; close (INF); $usrval = "$ENV{'REMOTE_ADDR'}:"; foreach $line (@access) { $num = length($line); for ($i = 0; $i < $num; $i++) { $temp = substr($line,$i,1); if ($temp eq ":") { $name = substr($line,$i+1,$num); } } } #******************************** print OUTF "<p>Posted By $name</p>"; print OUTF "<indented><p>$FORM{txtmsg}</p></indented>"; print "</body></html>"; close(OUTF); } sub AccessDenied { print "<html><head><title>You Are Not Logged In </title></head> <body bgcolor=\"black\" text=\"green\">"; print "<center>"; print "<h2>Sorry, You Must Sign In To Use This Service </h2> <p> $ENV{REMOTE_ADDR} </p> </center></body> </html>"; } </p> <p>signout.cgi</p> <p>#!/usr/bin/perl print "Content-type:text/html\n\n"; #********************************************************************** open(INF,"remoteaccess.log") or dienice("Failed To Open Remote Access $!"); @access = <INF>; close (INF); foreach $line (@access) { $usrval = "$ENV{'REMOTE_ADDR'}"; if($line =~ /$usrval/) { $line = ""; } @newAccess = (@newAccess,$line); } #Write Access Files system('echo "" > remoteaccess.log'); open(OUTF,">>remoteaccess.log") or dienice("Error: Cannot Open Remote Access Log : $!"); flock(OUTF,2); seek(OUTF,0,2); foreach $meh (@newAccess){ print OUTF "$meh"; close(OUTF); } print "<html><head><title>Signed Out</title></head><body>"; print "<body bgcolor=\"black\" text=\"green\"> <center>"; print "<h2>You Have Been Signed Out </h2>"; print "</center> </body></html>";

Now for the html source used for the website

<html> <head> <title>NetWork Control</title> <script LANGUAGE="JavaScript" type="text/javascript"> </script> </head> <body bgcolor="black" text="green" alink="black" vlink="black"> <font face="arial" size="8"></font> <center> <a href="register.html"><img src="register.jpg"></a> <a href="logon.html"> <img src="signin2.jpg"></a> <a href="forum.html"> <img src="forum.jpg"> </a> <a href="about.html"><img src="about.jpg"></a> <img src="cute_dragon.gif" align="right"> <br> <br> <br> <br> <br> <br> <img src="scrubnet.jpg" height="200" width="600"> <br><br> <img src="netcontrol.jpg"> <p>Local Net Control, For Yours And My Convienience</p> </center> </body> </html>

<html> <head> <title>Sign In </title> <!--My Appologies for my lazyness.. i'll write it better when i can be fucked --> </head> <body bgcolor="black" text="green" alink="black" vlink="black"> <center> <br><br><br><br><br><br><br><br><br><br><br><br> <form method="POST" action="cgi-bin/logon.cgi" name="logon"> Enter Username : <input type="text" name="username" size="20"> <br> Enter PassWord : <input type="text" name="password" size="20"> <br> <br> <input type="submit" value="Enter Room"> </form> </center> </body> </html>

<html> <head> <title>Message Boared</title> </head> <body bgcolor="black" text="green" alink="black" vlink="black"> <center> Enter Your Message And Press Send To Submit </center> <br> <a href="/cgi-bin/msgbrd.cgi"><img src="access.jpg"></a> <center> <form action="/cgi-bin/sendmsg.cgi" method="POST" name="msgbrd"> <textarea name="txtmsg" cols="30" rows="10"></textarea> <br> <input type="submit" value="Send!"> </form> <br> <br> </center> <img src="scrubnet.jpg" height="200" width="600"> <a href="/cgi-bin/signout.cgi"> <img src="signout.jpg" align="right"></a> </body> </html>

<html> <head><title>Registration</title></head> <body bgcolor="black" text="green"> <center> <p>Hi My Name Is Bob, Please Register To Use This Service</p> <form method="POST" action="cgi-bin/newuser.cgi" name="user"> Enter Username : <input type="text" name="username" size="20"> <br> Enter Password : <input type="text" name="password" size="20"> <br> <br> <input type="submit" value="Register Now"> </form> </center> </body> </html>

<html> <head><title>About</title></head> <body bgcolor="black" text="green"> <center> <br><br><br> <br><br><br><br><br><br> <br><br><br> <p>Hi My Name Is Bob, This Piece Of Crap You See Before You Is Just An Experiment To Develop Java Applets Multi Platfrom IRC Clients. This Site Is Still Under development </p> </center> </body> </html>



This all designed for a LAN but just change $remotehost variable to your desired address. Also it might be an idea to change user permitions on the scripts and access files.. eg) #chmod 755 *.pl for all the perl scripts, so the user can execute the file but no read or write to it. And for the access files, type #chmod 777 *.log.

I'll fix the webpage up later when i really could be fucked, atm im just a little drunk and am feeling sorry for myself.. oh well, im sure i'll get over it...