hi there
I’ve been writing a script to automate the process of creating a web folder online (for clients to see stuff) and then have it be password protected. Our server is using apache and as such i decided to utilize a htpasswd and htaccess file.
I’ve got the script mostly working - i can successfully create the online folder, create a preference file with the name of this folder locally (for use in other scripts) ask for a user name and password and have the script generate a passwd and htaccess file and upload them to the appropriate places on our server online.
however, i get the following error “The server encountered an internal error or misconfiguration and was unable to complete your request.” when going to a supposedly password protected url on our server. If i delete the .htaccess file just created in said folder, i can get access to it again - leading me to believe that at least the server is seeing the htaccess file and is trying to do something with it, but failing.
Interestingly enough if i manually type an .htaccess file in textedit, the above works fine - even if the contents of my manually typed file matches exactly the script generated one which fails.
My guess is that it must have something to do with the way the script is encoding the text in the file.
here’s the script:
--properties left blank for security
--i've left the .htaccess file as htaccess and have been manually adding the "." until testing is done :)
property webScripts : ""
property ftpInfo : "" -- in format ftp://user:pass@ftp.server.com:21
property passwdPath : ""
property clientPath : ""
property authUserPath : "/home/user/.htpasswds/clients/"
tell application "Finder"
activate
set localPath to container of (path to me) as string
repeat
display dialog "Web folder name: don't use '/' unless you mean to create a subfolder (eg parent/subfolder)" default answer "" buttons {"Cancel", "Create"} default button 2
set folderName to (text returned of result)
if result is not "" then
--changes any characters not found in okCharacters to make it websafe
set okCharacters to "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_/"
set allCharacters to characters of (folderName as text)
set characterList to allCharacters
repeat with k from 1 to (count allCharacters)
if (item k of characterList is in okCharacters) then
else
set item k of characterList to "_"
end if
end repeat
set okName to characterList as text
set folderName to okName
try
--once folderName is made websafe, create the folder online
do shell script "curl 'CWD " & clientPath & folderName & "' " & ftpInfo --checks to see if folder already exists online. If it does, start repeat over again
do shell script "curl -Q 'MKD " & clientPath & folderName & "' " & ftpInfo --creates the folder online
duplicate every file of folder webScripts to localPath
my createPrefFile(localPath, folderName)
set link to "http://www.headgearanimation.com/clients/" & folderName as string
display dialog "Client Folder created succesfully at " & link buttons {"Ok"} default button 1
set the clipboard to link
exit repeat
on error
display dialog "There's a problem with the name you entered. Does that folder already exist online?" buttons "Ok" default button 1 with icon caution
end try
else if result is "" then
display dialog "Please do not leave blank" buttons "Ok" default button 1 with icon caution
end if
end repeat
display dialog "Would you like to password protect this new folder?" buttons {"Cancel", "Yes"} default button "Yes"
if (button returned of result) is "Yes" then
my createPasswdFile(localPath, folderName)
end if
end tell
on createPrefFile(localPath, folderName)
--creates the text file
set prefFile to (((localPath) as string) & "_webdata.txt") as file specification
try
open for access prefFile with write permission
set eof of prefFile to 0
set folderName to folderName as text
write (folderName & return) to prefFile starting at eof
close access prefFile
on error
try
close access prefFile
end try
end try
end createPrefFile
on createPasswdFile(localPath, folderName)
tell application "Finder"
activate
repeat
display dialog "Please Enter Username" default answer "" buttons {"Cancel", "Continue."} default button 2
set theUsername to (text returned of result)
if theUsername is not "" then exit repeat
display dialog "Please do not leave blank" buttons "Ok" default button 1 with icon caution
end repeat
repeat
display dialog "Please Enter Password" default answer "" buttons {"Cancel", "Ok"} default button 2
set thePassword to (text returned of result)
if thePassword is not "" then exit repeat
display dialog "Please do not leave blank" buttons "Ok" default button 1 with icon caution
end repeat
set theResult to (do shell script "htpasswd -bmnd " & theUsername & " " & thePassword)
set passwdFile to (((localPath) as string) & "passwd") as file specification
try
open for access passwdFile with write permission
set eof of passwdFile to 0
write (theResult & return) to passwdFile starting at eof
close access passwdFile
on error
try
close access passwdFile
end try
end try
set htaccessFile to (((localPath) as string) & "htaccess") as file specification
try
open for access htaccessFile with write permission
set eof of htaccessFile to 0
write (return & "AuthType Basic" & return & return) to htaccessFile starting at eof
write ("AuthName \"Restricted Area\"" & return & return) to htaccessFile starting at eof
write ("AuthUserFile \"" & authUserPath & folderName & "/passwd\"" & return & return & return) to htaccessFile starting at eof
write ("require valid-user" & return) to htaccessFile starting at eof
close access htaccessFile
on error
try
close access htaccessFile
end try
end try
set passwdFile to POSIX path of passwdFile
do shell script "curl -Q 'MKD " & passwdPath & folderName & "' " & ftpInfo --creates the password folder online
do shell script "curl -T " & passwdFile & " " & ftpInfo & passwdPath & folderName & "/" --uploads the passwd file
set htaccessFile to POSIX path of htaccessFile
do shell script "curl -T " & htaccessFile & " " & ftpInfo & clientPath & folderName & "/" --uploads the .htaccess file
end tell
end createPasswdFile
and here is an example htaccess file from the script:
[b]AuthType Basic
AuthName “Restricted Area”
AuthUserFile “/home/user/.htpasswds/clients/job/passwd”
require valid-user[/b]
and here is one generated from the online tool from our host company:
[b]AuthType Basic
AuthName “Restricted Area”
AuthUserFile “/home/user/.htpasswds/clients/job/passwd”
require valid-user[/b]
Anyone have any tips at all as to what might be going on? Should i just be using some sort of shell script to write the htaccess file so its already in proper unix format and save it somewhere specific for me? same for the password file - could i just echo the results of the htpasswd command to a file at a set location?
any help would be greatly appreciated