First off, a few details. I am running IIS 6 and perl 5.8.4 on my webserver. What my internal website is SUPPOSED to do is allow a user to enter a computer name on a previous HTML page which passes that variable (strComputer) to the script which will query a MS SQL database for the encrypted password, the script will then decrypt the password and output it in a new browser window.
The problem is that the very first time I open my browser (tried both IE and Firefox with same results) and enter a computer name, the script always returns "-1" as the password. If I close the output window and re-submit (or enter a new computer name), it will return the correct password. I've included my code and I'm hoping someone can shed some light on my problem. For this posting, I did change the encryption key and server names that I am using.
use Win32::OLE qw (in);
use CGI qw(:standard);
$Win32::OLE::Warn = 3;
print header;
print start_html { title => 'Password Query' };
my $sqlserver = "SERVER_NAME";
my $database = "PASS";
my $pwdtble = "_PASSWORDS";
my $key = "AB928RQG";
my $target = param('strComputer');
# ERROR CHECKING
if ($target eq ""){
print "<br><center><strong>You must enter a computer account.</str
+ong></center><br>";
print "<p align='center'><input name='Close' type='button' id='Clo
+se' onClick='window.close()' value='Close'></p>";
print end_html;
exit 1;
}
my $text = retrieve($target);
print "<h2>Password: $text</h2><br></center>";
print "<p><center><input type='button' value='Close' name='close' onCl
+ick='window.close()'></center></p>";
print end_html;
sub retrieve{
my $target = shift @_;
use DBD::ODBC;
my %history;
my $DSN = "driver={SQL Server};Server=$sqlserver;database=$databas
+e";
my $dbh = DBI->connect("dbi:ODBC:$DSN") || return -1;
if (not $dbh) { print "coud not connect\n"; return -1; }
my $sth = $dbh->prepare("select password, date_modified from $pwdt
+ble where computername = ?");
if ($sth->execute($target))
{
while (my ($password, $date) = $sth->fetchrow())
{ $history{$date} = decrypt($key,$password); }
}
else
{ return; }
foreach my $date (reverse (sort (keys %history)))
{
return $history{$date};
}
}
sub decrypt{
use Crypt::TripleDES;
my $key = shift @_;
my $e_text = shift @_;
my $des = new Crypt::TripleDES;
my $text = $des->decrypt3 ( $e_text, $key );
return $text;
}
I've been pulling my hair out trying to figure this out. Any help would be greatly appreciated.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.