Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Perl/CGI and first query
by insaniac (Friar) on Mar 10, 2005 at 15:24 UTC
    maybe the -1 is there because it did not find anything the first time, and your code says at a certain point in the sub retrieve:
    return -1
    and if you store that in $text and afterwards you try to print it.. you'll get troubles ;-)

    so i'm guessing your db connection is not working...

    --
    to ask a question is a moment of shame
    to remain ignorant is a lifelong shame