#!/usr/bin/php ($time - ($timeframe*60))) { if(($fp) && (strlen($otp_array[$i]) > 5)) { fwrite($fp,$otp_array[$i]."\n"); } // If the typed OTP has been already used for this user in the allowed timeframe, it's an error. if ($user == $search[0]) { if(trim(strtolower($search[1])) == trim(strtolower($otp))) { $otp_used = true; } } } } fclose($fp); if ($otp_used) { // print "This OTP has been used before"; return(5); } // Now we check if the typed OTP is valid for($i = $time + ($offset * 10) - $maxperiod; $i <= $time + ($offset * 10) + $maxperiod; $i++) { $md5 = substr(md5(substr($i,0,-1).$initsecret.$pin),0,6); if($otp == $md5) { // Write $opt to CSV file @$fp = fopen($used_otp_file,"ab"); if($fp) { fwrite($fp,$user.$delimiter.$otp.$delimiter.$time."\n"); // Write $user|$otp|$time| to the file fclose($fp); // Close the file } // print "This OTP WORKED!!!"; return(0); } } // print "OTP was not valid"; return(1); } // Function to generate an OTP based on the init-secret and the PIN function generateOTP($initsecret,$pin) { return substr(md5(substr(gmdate("U"),0,-1).$initsecret.$pin),0,6); } /************************************************************************************************** * Here is the main code, only if the otpverify.php file is used as a command-line for FreeRADIUS * **************************************************************************************************/ // With two parameters (3 because parameters 0 in the full path of the actual script), // we generate a new OTP based on the init-secret and the pin if (3 == count($argv)) { echo generateOTP($argv[1],$argv[2])."\n"; } elseif (6 != count($argv)) { echo "USAGE: otpverify.php Username OTP Init-Secret PIN Offset\n"; exit(4); } else { $result = (checkOTP($argv[1],$argv[2],$argv[3],$argv[4],$argv[5])); if (0 == $result) { echo "ACCEPT\n"; } else { echo "FAIL\n"; } exit($result); }