;Name: firefox_ntlm_preference_conf.vbs
;Author: Matt Martz
;Date: 2007-05-29
;
;This script is designed to allow an organization to manage a list of sites that Firefox will send
;NTLM authentication to. This list is similar to the Local Intranet zone located in the Internet
;Explorer configuration options. With this preference enabled users will be automatically
;authenticated to sites which Internet Explorer would automatically be authenticated to.
;
;Note from the author:
; 1) I don't claim that this (script) is well written. It started as a script to just append the
; proposed site list to the end of the file and grew into a beast that would add the proposed
; site list and and sites that were configured by the user that didn't exist in the proposed
; site list
; 2) VBScript sucks at parsing and editing files. It wasn't meant for this purpose but with
; some innovation can be made to do what you want. Once again this is sloppy code and it
; would be more efficient written in a different language. But VBScript is natively supported
; on Windows which makes it easy for group policy deployment
; 3) I'm sure something else sucks about this script but I just haven't found it yet...So if you
; find something else that sucks you can let me know at the above email address and I'll see
; if I can make it suck less.
;
;Define Variables
$=SetOption('Explicit','On')
Dim $fsObj, $WshShell, $fullpath, $SubFolders, $prefsfile, $strLine
Dim $strSiteList, $strAppData, $strFireProf, $strProfFolder, $strSearchString
Dim $Folder, $i, $strNewContents
Dim $intMatchNTLM, $strLineResult, $intMatchLine, $strAddExistSiteList
Dim $arrCurSiteList, $intCurSiteArrayLength, $strExistSiteList
Dim $arrSiteList, $intSiteArrayLength, $i_addsite, $intExistSiteListLen
;Configure Environment
$fsObj = CreateObject("Scripting.FileSystemObject")
$WshShell = CreateObject( "WScript.Shell" )
;Define the path to the profiles directory for Firefox
$strAppData = $WshShell.ExpandEnvironmentStrings("%APPDATA%")
$strFireProf = "\Mozilla\Firefox\Profiles\"
;Check if firefox even has a profiles directory, if not than quit
If NOT $fsObj.FolderExists($strAppData + $strFireProf)
Exit 0
EndIf
;Define the proposed site list, split it into an array and determine length of array
;$strSiteList should be a list of sites beginning with http:// or https://
;Only the DNS portion of the site should follow and there should be no trailing slash
;THIS ($strSiteList) SHOULD BE THE ONLY VARIABLE THAT NEEDS CONFIGURATION
$strSiteList = "http://10.1.0.154:8080/more/"
$arrSiteList = Split($strSiteList, ",", -1, 1)
$intSiteArrayLength = UBound($arrSiteList)
;Create an array of the subfolders in the profiles directory (there should only be one)
$fullpath = $fsObj.GetFolder($strAppData + $strFireProf)
$SubFolders = $fullpath.subfolders
;Set the full path of the prefs.js file
For Each $Folder in $SubFolders
$strProfFolder=$Folder
$prefsfile = $fsObj.OpenTextFile($Folder + "\prefs.js", 1, 1)
Next
;Lets load the prefsfile as a string for easy searching and then search for the preference we are looking for
;If found $intMatchNTLM will be greater than 0
$strSearchString = $prefsfile.ReadAll
$intMatchNTLM = InStr($strSearchString, "network.automatic-ntlm-auth.trusted-uris")
$prefsfile.close
;Lets reopen the prefs.js file so we are at the beginning
$prefsfile = $fsObj.OpenTextFile($strProfFolder + "\prefs.js", 1, 1)
;Now we do a while loop to parse the file and look for the ntlm preference
While $prefsfile.AtEndOfStream = 0
;Read in the current line and check for presence of the preference name
;If found $intMatchLine will be greater than 0
$strLineResult = $prefsfile.ReadLine
$intMatchLine = InStr($strLineResult, "network.automatic-ntlm-auth.trusted-uris")
;Check if the preference exists and if so...
If $intMatchLine>0
;Format the string, removing unwanted characters so delimiting is easier and convert to an array
$strLineResult = Replace($strLineResult, chr(34), "")
$strLineResult = Replace($strLineResult, ")", "")
$strLineResult = Replace($strLineResult, ";", "")
$arrCurSiteList = Split($strLineResult, ",", -1, 1)
;Get the length of the array and loop it to create a list of sites that are in the existing list but not in the proposed list
$intCurSiteArrayLength = UBound($arrCurSiteList)
For i=1 to $intCurSiteArrayLength
$strExistSiteList = $strExistSiteList + "," + $arrCurSiteList[$i]
Next
$intExistSiteListLen = Len($strExistSiteList)
$strExistSiteList = Right($strExistSiteList, $intExistSiteListLen-2)
$strAddExistSiteList = $strExistSiteList
For $i_addsite=0 to $intSiteArrayLength
$strAddExistSiteList = Replace($strAddExistSiteList,$arrSiteList[$i_addsite], "")
Next
;Continuing with the above comment we need to check for formatting issues caused by removing records and fix them
$strAddExistSiteList = Replace($strAddExistSiteList,",,", ",")
If InStr($strAddExistSiteList, ",") = 1
$strAddExistSiteList = Right($strAddExistSiteList, Len($strAddExistSiteList)-1)
EndIf
If InStrRev($strAddExistSiteList, ",") = Len($strAddExistSiteList) and InStrRev($strAddExistSiteList, ",") <> 0
$strAddExistSiteList = Left($strAddExistSiteList, Len($strAddExistSiteList)-1)
EndIf
If Len($strAddExistSiteList) = 0
$strSiteList = $strSiteList
$strSiteList = Replace($strSiteList,",,", ",")
Else
$strSiteList = $strSiteList + "," + $strAddExistSiteList
$strSiteList = Replace($strSiteList,",,", ",")
EndIf
EndIf
Loop
;Now that we have the list of sites to match the proposed list plus the sites that didn't overlap between existing and proposed
;If the preference was not set at all we add just the proposed list
If $intMatchNTLM<1
;Open the file for appending as we aren't editing the contents
$prefsfile = $fsObj.OpenTextFile($strProfFolder + "\prefs.js", 8, 1)
$prefsfile.writeline("user_pref(" + chr(34) + "network.automatic-ntlm-auth.trusted-uris" + chr(34) + ", " + chr(34) + $strSiteList + chr(34) + ");")
$prefsfile.close
Else
;If the existing list is longer than the proposed list and the existing list contains sites then quit
If Len($strExistSiteList) >= Len($strSiteList) and Len($strAddExistSiteList) = 0
Exit 0
;else lets check to see how we add the list
Else
;Open the file for reading
$prefsfile = $fsObj.OpenTextFile($strProfFolder + "\prefs.js", 1, 1)
;Loop the file writing all lines excluding the preference we are writing to a string for easy manipulation
Do Until $prefsfile.AtEndOfStream
$strLine = $prefsfile.Readline
$strLine = Trim($strLine)
If $strLine <> "user_pref(" + chr(34) + "network.automatic-ntlm-auth.trusted-uris" + chr(34) + ", " + chr(34) + $strExistSiteList + chr(34) + ");"
$strNewContents = $strNewContents + $strLine + @CRLF
EndIf
Loop
$prefsfile.close
;Lets reopen the file in editing mode and write the contents of the file minus the preference we are setting
$prefsfile = $fsObj.OpenTextFile($strProfFolder + "\prefs.js", 2, 1)
$prefsfile.write($strNewContents)
$prefsfile.close
;Now lets reopen the file in append mode and append the preference we are setting to the end
$prefsfile = $fsObj.OpenTextFile($strProfFolder + "\prefs.js", 8, 1)
$prefsfile.writeline("user_pref(" + chr(34) + "network.automatic-ntlm-auth.trusted-uris" + chr(34) + ", " + chr(34) + $strSiteList + chr(34) + ");")
$prefsfile.close
EndIf
EndIf