#
# Check for anonymous ftp and world writable directories
#

require 'sys/socket.ph';

$destination = "localhost";

$warn     = 0;
$fail     = 0;
$anonftp  = 0;
$writable = 0;

&pcheck("Anonymous FTP");

$result = &ConnectFTP();

if  ($result == 1) {

	$line = <S>;
	while ($line =~ /^\d+-/) { $line = <S>; }

	send(S,"USER ANONYMOUS\n",NULL);
	$result = <S>;
	send(S,"PASS secur@$hostname.here\r\n",NULL);
	$result = <S>;

	if ($result =~ /^230/) {
		$anonftp = 1;
		while ($result =~ /^230-/) { $result = <S>; }

		send(S,"STOR .securscan\n",NULL);

		$result = <S>;
		if (!($result =~ /^5/)) {
			$writable = 1;
			send(S,"DELE .securscan\n",NULL);
			$result = <S>;
		}
	}
} elsif ($result == -1) {
	&report("INFO: Host not running ftpd.\n");
} else {
	&report("ERROR:  Network error.\n");
	&error;
	exit;
}
	
send(S,"QUIT\n",NULL);
close(S);


if ($anonftp) {
	&report("WARNING: Machine supports anonymous ftp.\n");
	$warn = 1;
} else {
	&report("INFO: This machine does not support anonymous ftp.\n");
}

if ($writable) {
	&report("FAILURE: Machine has a world writable home directory.\n");
	$fail = 1;
}


if ($fail) { 
	&failed;
} elsif ($warn) {
	&warn;
} else {
	&passed;
}

exit 0;

sub ConnectFTP {

	$port = 21;
        $SOCK_STREAM = &'SOCK_STREAM;
        $AF_INET = &'AF_INET;
        $sockaddr = 'S n a4 x8';

        ($name,$aliases,$proto) = getprotobyname('tcp');

        ($name,$aliases,$type,$len,$thisaddr) = gethostbyname($hostname);
        ($name,$aliases,$type,$len,$thataddr) = gethostbyname($destination);

        $this=pack($sockaddr,$AF_INET,0,$thisaddr);
        $that=pack($sockaddr,$AF_INET,$port,$thataddr);

        if (!(socket(S, $AF_INET, $SOCK_STREAM, $proto))) { return -2; }

        if (!(bind(S,$this))) { return -2; }

        if (!(connect(S,$that))) { return -1; }

	return 1;
}
