#!/usr/bin/perl
# $Id: challenge.cgi,v 2.6 2020/03/23 21:59:22 jeff Exp $
use strict;
use Socket;
use CGI;

sub get_response
{
my $server_ip_address = inet_aton('ll.arpa.net') or return '';
my $server_port = (getservbyname('nntp', 'tcp') or 119);
socket(REMOTE, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or return '';
select REMOTE; $| = 1; select STDOUT;	# make network writes unbuffered
alarm 600;
connect(REMOTE, sockaddr_in($server_port, $server_ip_address)) or return '';
$SIG{PIPE} = 'IGNORE';
print REMOTE @_ or return '';
shutdown(REMOTE, 1) or return '';
local $/ = undef;
$_ = <REMOTE>;
close REMOTE;
$SIG{PIPE} = 'DEFAULT';
return $_;
}

print << 'EOF';
Content-type: text/html

<HTML>
<HEAD>
<TITLE>Alcatel Challenge/Response</TITLE>
</HEAD>

<BODY BGCOLOR="#FFFFFF" LINK="#336699" VLINK="#996633" ALINK="#FF6699">
<IMG SRC="/images/security.gif" WIDTH=280 HEIGHT=70 BORDER=0 ALT="Security">
<CENTER><H2>Alcatel "EXPERT" Mode Challenge/Response</H2></CENTER>

This page computes and displays the appropriate response to
the challenge string presented by an Alcatel ADSL modem when
entering "EXPERT" mode from the command line interpreter.<P>

EOF

my $particle = 'the';
my $c = CGI::param('c'); CGI::delete('c');
if (defined $c and length $c and defined $ENV{'REQUEST_METHOD'} and $ENV{'REQUEST_METHOD'} eq 'POST') {
	print '<P><CENTER><TABLE><TR><TD BGCOLOR="#880000"><FONT FACE="Arial, Helvetica" SIZE=+1 COLOR="#FFFFFF">';
	my $agreements = 0;
	foreach my $i (1 .. 7) {
		$_ = CGI::param("q$i");
		if (defined) {
			$agreements++ if ($_ eq 'y');
			CGI::delete("q$i");
			}
		}
	if (defined CGI::param) {
		print "Unexpected CGI parameters.<BR>\nPlease do not hack this script.";
		}
	elsif ($agreements != 7) {
		print 'You must agree to ALL of the statements below.';
		}
	elsif ($c =~ /^[- A-Za-z0-9\(\)\']{20,40}$/) {
		my $response = get_response $c;
		if (defined $response) {
			if (length $response) {
				print "For challenge string\n<BIG><B>$c</B></BIG><BR>\nthe response is <BIG><B>$response</B></BIG>";
				$particle = 'another';
				}
			else {
				print "Server failure while computing the response.\nTry again later.";
				}
			}
		else {
			print "Unable to determine the response for challenge string\n<BIG><B>$c</B></BIG><BR>\nYou may have typed it incorrectly or your modem may be too new for our algorithm.";
			}
		}
	else {
		print "Syntax error in challenge string\n<BIG><B>$c</B></BIG>";
		}
	print "</TD></TR></TABLE></CENTER>\n\n";
	}

print "<FORM METHOD=POST ACTION=\"challenge.cgi\">\n\nTo receive $particle \"EXPERT\" mode response you must agree to the following\nstatements.<P>\n";
print << 'EOF';

<TABLE BORDER=1 CELLPADDING=3>
<TR><TD BGCOLOR="#DDDDDD"><INPUT TYPE=radio NAME=q1 VALUE="y">&nbsp;YES<BR>
<INPUT TYPE=radio NAME=q1 VALUE="n" CHECKED>&nbsp;NO</TD>
<TD BGCOLOR="#DDDDDD">I understand that "EXPERT" mode exists primarily
for (Alcatel) experts to perform low level troubleshooting.  Use of
this mode can cause loss of service, or damage to or destruction of
the Speed Touch device.  It was not intended for end-user use.</TD></TR>

<TR><TD BGCOLOR="#DDDDDD"><INPUT TYPE=radio NAME=q2 VALUE="y">&nbsp;YES<BR>
<INPUT TYPE=radio NAME=q2 VALUE="n" CHECKED>&nbsp;NO</TD>
<TD BGCOLOR="#DDDDDD">I understand that "EXPERT" mode provides
facilities by which the device can easily be damaged or rendered
permanently useless, either accidentally or intentionally.  I accept
full responsibility for any damage or destruction that may result from
my use of this information.</TD></TR>

<TR><TD BGCOLOR="#DDDDDD"><INPUT TYPE=radio NAME=q3 VALUE="y">&nbsp;YES<BR>
<INPUT TYPE=radio NAME=q3 VALUE="n" CHECKED>&nbsp;NO</TD>
<TD BGCOLOR="#DDDDDD">I choose to use this information at <B>MY own
risk</B>, and that my service provider may then refuse to give me any
support, regardless of whether or not it disrupts my service or
renders my equipment unusable.</TD></TR>

<TR><TD BGCOLOR="#DDDDDD"><INPUT TYPE=radio NAME=q4 VALUE="y">&nbsp;YES<BR>
<INPUT TYPE=radio NAME=q4 VALUE="n" CHECKED>&nbsp;NO</TD>
<TD BGCOLOR="#DDDDDD">I understand that using this information may
void my warranty or be in violation of my contractual agreement with
my service provider.</TD></TR>

<TR><TD BGCOLOR="#DDDDDD"><INPUT TYPE=radio NAME=q5 VALUE="y">&nbsp;YES<BR>
<INPUT TYPE=radio NAME=q5 VALUE="n" CHECKED>&nbsp;NO</TD>
<TD BGCOLOR="#DDDDDD">I will not use -- nor permit others to use --
this information to gain unauthorized access to any Speed Touch, 1000
ADSL, or similar device.</TD></TR>

<TR><TD BGCOLOR="#DDDDDD"><INPUT TYPE=radio NAME=q6 VALUE="y">&nbsp;YES<BR>
<INPUT TYPE=radio NAME=q6 VALUE="n" CHECKED>&nbsp;NO</TD>
<TD BGCOLOR="#DDDDDD">I absolve SDSC and the information providers
from any responsibility for loss, damage or inconvenience, or breach
of contract that may result from my use of this information.</TD></TR>

<TR><TD BGCOLOR="#DDDDDD"><INPUT TYPE=radio NAME=q7 VALUE="y">&nbsp;YES<BR>
<INPUT TYPE=radio NAME=q7 VALUE="n" CHECKED>&nbsp;NO</TD>
<TD BGCOLOR="#DDDDDD">THIS INFORMATION AND/OR SOFTWARE IS PROVIDED BY
THE AUTHOR OF THIS ARCHIVE "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
I ABSOLVE THE AUTHORS, SDSC, AND UCSD FROM ANY LIABILITY FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<P>

IN NO EVENT SHALL THE PARTIES DESCRIBED ABOVE BE LIABLE FOR THIRD
PARTY INTERFERENCE WITH CONTRACTUAL RELATIONS AS BETWEEN MYSELF, MY
SERVICE PROVIDER, AND/OR ALCATEL.</TD></TR>
</TABLE>
<P>
<TABLE BORDER=0><TR>
<TD ALIGN=right>Enter challenge string:</TD>
<TD><INPUT TYPE=text NAME=c SIZE=40></TD>
</TR><TR>
<TD ALIGN=right><SMALL>Example:</SMALL></TD>
<TD><SMALL>'SpeedTouch (00-90-D0-00-00-00)'</SMALL></TD>
</TR><TR>
<TD></TD>
<TD ALIGN=center><INPUT TYPE=submit VALUE="Submit"> <INPUT TYPE=reset VALUE="Clear"></TD>
</TR></TABLE>
</FORM>

<P><CENTER>[ <A HREF="index.shtml">return to Alcatel self-help guide</A> ]</CENTER>
</BODY>
</HTML>
EOF
1;
