Perl - Ensuring a Modest Amount of Security

Perl has a number of operators (twenty-seven of them, I believe) that work specifically with files to determine information such as the owner of a file, file size, time file was last accessed, etc.  We'll see more of these operators in a later lesson.  The one that we want to introduce in this lesson - -M - tells us the age in days since a file or filehandle has last been modified.  Since we're already looking at the file in the init-words() subroutine, it makes sense to use this operator there to see whether its value is greater than seven for the WORDSLIST filehandle. Remember that the Chief Director for Secret Word lists said, "That secret word list has got to change at least once a week!"

Here's a small chunk of code to do that:

sub init_words {
	open (WORDSLIST, "wordslist") || die "can't open wordslist: $!";
	if (-M WORDSLIST >= 7.0) {			# comply with bureaucratic policy
		die "Sorry, the wordslist is older than seven days!";
	}
	while (defined ($name = <WORDSLIST>)) {
		chomp ($name);
		$word = <WORDSLIST>;
		chomp ($word);
		$words($name) = $word;
	}
	close (WORDSLIST) || die "couldn't close wordslist: $!";
}

The value of -M WORDSLIST is compared to seven, and if greater, bingo, we've violated policy.

The rest of the program remains unchanged, so I won't repeat it here in this lesson.

You know, we should really watch for "break-in" attempts when someone is guessing the secret word.  If we were using a UNIX system, we'd probably use the mail command to send an email message to someone about an incorrect guess (failed attempt).  However, in Windows, there is no standard mail command, so let's log incorrect guesses (failures) to a file instead.

What do you suppose the programming elements are that will allow you to do this in Perl?

See you next lesson!