#!/usr/bin/perl -w use CGI qw(:cgi); use File::Basename; use File::Copy qw(mv); use FindBin; use Digest::MD5 qw(md5_hex); use MIME::Base64; # BASEPATH must be the location of this cgi, and other source trees. $BASEPATH = $FindBin::Bin; chdir ($BASEPATH); $GAMENAME = "gamename"; $USERDATFILE = "/var/lib/$GAMENAME/$GAMENAME.csv"; $LOGFILE = "/var/log/$GAMENAME.log"; $CONFFILE = "/etc/$GAMENAME.conf"; $LOCKFILE = "/var/lock/$GAMENAME.lck"; $CONTACTTO = 'webmaster@your.maker.com'; ($sec,$min,$hour,$day,$month,$year,$tmp) = localtime(time); $tmp++; # avoid worning of "unused value $tmp" $TIMESTR = sprintf("%d/%02d/%02d_%02d:%02d:%02d", $year+1900, $month+1, $day, $hour, $min, $sec); # include $CONFFILE for general configuration require "$CONFFILE" if (-f "$CONFFILE"); # print the page with message if there is a message sub print_page { my ($mes) = $_[0]; my ($mail1, $mail2, $uniq1) = ("","",""); $mail1 = $mailaddr if (defined $mailaddr); $mail2 = $mailaddr2 if (defined $mailaddr2); $uniq1 = $uniqkey1 if (defined $uniqkey1); print < GAMENAME activation page
Input your e-mail address and Unique Key 1
$mes

Mail Address :
Mail Address(confirm):
Unique Key 1:

EOM ; } # log a message(no error check to avoid infinite loop) sub errlog { my ($mes) = $_[0]; open LOG, ">>$LOGFILE"; flock(LOG, 2); print LOG "$TIMESTR $mes\n"; close LOG; } # print error message and the web page and exit. sub error_exit { my $mes = $_[0]; errlog($mes); $mes = "
Error: $mes\n

\n"; print_page($mes); unlockfile(); exit 1; } # lock/unlock the $LOCKFILE for exclusive work sub lockfile { open LOCK, ">$LOCKFILE" or error_exit("Internal error."); flock(LOCK, 2); } sub unlockfile { close LOCK; } lockfile(); # read/check 3 parameters which are specified by a user. $cgiq = new CGI(); # print default page, when no arguments are specified. if (scalar($cgiq->param) <= 0) { print_page(""); exit 0; } $mailaddr = $cgiq->param("MAILADDR1"); $mailaddr2 = $cgiq->param("MAILADDR2"); $uniqkey1 = $cgiq->param("UNIQKEY1"); if (!defined($mailaddr) || !defined($mailaddr2) || $mailaddr eq "" || $mailaddr2 eq "") { error_exit("Please input mail addresses."); } if ($mailaddr ne $mailaddr2) { error_exit("Mail addresses miss match."); } if (scalar(@_ = split(/\@/, $mailaddr)) != 2) { error_exit("Mail address is wrong($mailaddr)."); } if (!defined($uniqkey1) || $uniqkey1 eq "") { error_exit("Please input Unique Key 1"); } # get encoded mail address, for security. $encmailaddr = encode_base64($mailaddr, ""); $encmailaddr = encode_base64($encmailaddr, ""); $encmailaddr = encode_base64($encmailaddr, ""); # configure %usrhash as usrhash of $USERDATFILE %usrhash = (); open FILE, "<$USERDATFILE" or error_exit "Failed to read-open data file."; while() { chomp; ($key1hash, $uniqkey2, $mail, $other) = split(/,[ \t]*/, $_); $usrhash{$key1hash} = [$key1hash, $uniqkey2, $mail, $other]; } close FILE; # get/check the current user data $key1hash = md5_hex($uniqkey1); if (!defined($usrhash{$key1hash})) { error_exit("Wrong unique key 1($uniqkey1)."); } ($key1hash, $uniqkey2, $mail, $other) = @{$usrhash{$key1hash}}; if ($mail ne "" && $mail ne $encmailaddr) { error_exit("Another user using this unique key 1($uniqkey1)."); } # update the user data $usrhash{$key1hash}->[2] = $encmailaddr; # write the user data onto $USERDATFILE open FILE, ">$USERDATFILE.tmp" or error_exit "Failed to write-open data file."; foreach $key (keys(%usrhash)) { print FILE join(", ", @{$usrhash{$key}})."\n"; } close FILE; $obj = $TIMESTR; $obj =~ s/\//_/g; mv ($USERDATFILE, $USERDATFILE."_".$obj) or error_exit "File move failed1."; mv ("$USERDATFILE.tmp", $USERDATFILE) or error_exit "File move failed2."; # send mail to $user with $uniqkey2 # echo "UniqKey2 = $uniqkey2" | mail -s "UniqKey2 for $GAMENAME" -r "$CONTACTTO" $mailaddr unlockfile(); print < GAMENAME activation page

Your Uniq Key 2 has been sent to "$mailaddr" via e-mail.

Please check your mailbox.
If you have any troubles, please contact to $CONTACTTO with:

  1. what kind of trouble you have (described specificly)
  2. your mail address which was input to the activation form
  3. your unique key 1
EOM ; exit 0;