# This program demonstrates user input, string manipulation, basic # control statements, file manipulation, and the mail command. # # The program asks for a the user to enter their name. If # the user's name is randal then the user is greeted otherwise, # the user is asked to enter a secret word which must correspond # to an entry in a preexising password file. If the user fails # to enter the correct password the the program sends mail to # the programs's author notifying him of the users's failed attempt # to login. init_words(); print "what is your name? "; $name = ; chomp($name); if ($name =~ /^randal\b/i) { # back to the other way :-) print "Hello, Randal! How good of you to be here!\n"; } else { print "Hello, $name!\n"; # ordinary greeting print "What is the secret word? "; $guess = ; chomp $guess; while (! good_word($name,$guess)) { print "Wrong, try again. What is the secret word? "; $guess = ; chomp $guess; } } dbmopen (%last_good,"lastdb",0666); $last_good{$name} = time; dbmclose (%last_good); sub init_words { while ($filename = <*.secret>) { open (WORDSLIST, $filename)|| die "can't open $filename: $!"; if (-M WORDSLIST < 7.0) { while ($name = ) { chomp ($name); $word = ; chomp ($word); $words{$name} = $word; } } else { # rename the file so it gets noticed rename ($filename,"$filename.old") || die "can't rename $filename.old: $!"; } close WORDSLIST; } } sub good_word { my($somename,$someguess) = @_; # name the parameters $somename =~ s/\W.*//; # delete everything after first word $somename =~ tr/A-Z/a-z/; # lowercase everything if ($somename eq "randal") { # should not need to guess return 1; # return value is true } elsif (($words{$somename} || "groucho") eq $someguess) { return 1; # return value is true } else { open (MAIL, "|mail YOUR_ADDRESS_HERE"); print MAIL "bad news: $somename guessed $someguess\n"; close MAIL; return 0; # return value is false } }