# Loads the password file # Returns full hash if it exists and there are entries and it can read them # Returns empty hash if one of the three conditions are false sub LoadAuthFile { local ($FileName) = @_; local (%Data, $Line, $User, $Pass); open(FILE, $FileName) || return %Data; foreach $Line () { chomp($Line); ($User, $Pass) = split(/:/, $Line, 2); $Data{$User} = $Pass; } close(FILE); return %Data; } # Saves an authorization file # Returns 0 if everything went well # Returns 1 if it could not write to the file sub SaveAuthFile { local ($FileName, %Data) = @_; open (FILE, "> $FileName") || return 1; foreach $key (sort(keys(%Data))) { print FILE "$key:$Data{$key}\n"; } close (FILE); return 0; } # Creates the .htaccess file to make a directory password protected # $DirName must NOT have a slash at the end of it # Returns a 1 if everything went well # Returns a 0 if it could not write the file sub MakeRestrictedFile { local ($DirName, $PasswordFile, $Title) = @_; open (FILE, "> $DirName/.htaccess") || return 0; print FILE "AuthName \"$Title\"\n"; print FILE "AuthType Basic\n"; print FILE "AuthUserFile $PasswordFile\n"; print FILE "\n"; print FILE "require valid-user\n"; close (FILE); return 1; } # Initialize the random number seed srand ($$); # Encrypts a password with a random key # Returns the crypt()'d form of the password with a random key sub EncryptPassword { local ($Password) = @_; local ($Key1, $Key2, $Key); $Key1 = int(rand(62)); $Key2 = int(rand(62)); $Key1 += 6 if ($Key1 > 35); $Key1 += 7 if ($Key1 > 9); $Key1 += 48; $Key2 += 6 if ($Key2 > 35); $Key2 += 7 if ($Key2 > 9); $Key2 += 48; $Key = chr($Key1) . chr($Key2); return crypt($Password, $Key); } 1; # Must be last line