c009e2aa877974f1dff61f9d834ee3d36271d8fb
[shibboleth/cpp-sp.git] / msi / scripts / shib_uninstall_isapi_filter.vbs
1 Sub DeleteISAPIFilters(IISPath,dllPath)
2
3   Dim filter, FiltersObj, LoadOrder, FilterArray, FilterItem
4
5   Set FiltersObj = GetObject(IISPath & "/Filters")
6   LoadOrder = FiltersObj.FilterLoadOrder
7
8   for each filter in FiltersObj
9     if (filter.Class = "IIsFilter") then
10       if (filter.FilterPath = dllPath) then
11
12         'Delete the found filter here
13         'If there's anything to potentially delete...
14         if (LoadOrder <> "") then
15           FilterArray = split(LoadOrder,",")
16           LoadOrder = ""
17           for each FilterItem in FilterArray
18             if (FilterItem <> filter.Name) then
19               LoadOrder = LoadOrder & FilterItem & ","
20             end if
21           next
22           'Remove trailing comma if any filters were kept
23           if (LoadOrder <> "") then
24             LoadOrder = mid(LoadOrder,1,len(LoadOrder)-1)
25           end if
26
27           'Set the Load Order to the new shibboleth-less order
28           if (FiltersObj.FilterLoadOrder <> LoadOrder) then
29             FiltersObj.FilterLoadOrder = LoadOrder
30             FiltersObj.SetInfo
31           end if
32         end if
33
34         'Delete the actual IISFilter object
35         FiltersObj.Delete "IIsFilter",filter.Name
36
37       end if
38     end if
39   next
40
41 End Sub
42
43
44 Sub DeleteFileExtensions(siteObj, dllPath)
45
46 Dim ScriptMaps, newScriptMaps
47 Dim line, lineArray, lineIndex
48 Dim fileExtension
49 Dim existsFlag
50
51   ScriptMaps = siteObj.ScriptMaps
52   redim newScriptMaps(0)
53   lineIndex = 0
54   'copy each entry from the old ScriptMaps to newScriptMaps
55   'unless it is for dllPath
56   for each line in ScriptMaps
57     lineArray = split(line,",")
58     if (lineArray(1) <> dllPath) then
59       redim preserve newScriptMaps(lineIndex)
60       newScriptMaps(lineIndex) = line
61       lineIndex = lineIndex + 1
62     else
63       existsFlag = "exists"
64     end if
65   next
66   'If we found dllPath, then use the newScriptMaps instead
67   if (existsFlag = "exists") then
68     siteObj.ScriptMaps = newScriptMaps
69     siteObj.SetInfo
70   end if
71
72 End Sub
73
74
75 '*** Begin Main Code ***
76 Dim WebObj
77 Dim InstallDir
78 Dim ShibISAPIPath
79 Dim site, siteObj, sitePath
80
81 ' First of all look for the FileExtension
82 Set WshShell = CreateObject("WScript.Shell")
83 On Error Resume Next
84 regValue = WshShell.RegRead("HKLM\SOFTWARE\Shibboleth\FileExtension")
85 if (Err = 0) then
86   ' Registry key is still there - this is an upgrade, so exit
87
88 else 
89   ' Key is gone - a pure uninstall
90
91   'Don't show errors, we'll handle anything important
92   On Error Resume Next
93
94   'Attempt to get W3SVC.  If failure, end script (e.g. IIS isn't available)
95   Set WebObj = GetObject("IIS://LocalHost/W3SVC")
96   if (Err = 0) then
97
98     'Get the INSTALLDIR value via CustomActionData
99     InstallDir = Session.Property("CustomActionData")
100
101     'Remove all trailing backslashes to normalize
102     do while (mid(InstallDir,Len(InstallDir),1) = "\")
103       InstallDir = mid(InstallDir,1,Len(InstallDir)-1)
104     loop
105     ShibISAPIPath = InstallDir & "\lib\shibboleth\isapi_shib.dll"
106
107     'Delete ISAPI Filter
108     'First do the master service
109     DeleteISAPIFilters "IIS://LocalHost/W3SVC",ShibISAPIPath
110     'Now do the websites
111     for each site in WebObj
112       if (site.Class = "IIsWebServer") then
113         sitePath = "IIS://LocalHost/W3SVC/" & site.Name
114         DeleteISAPIFilters sitePath,ShibISAPIPath
115       end if
116     next
117
118     'Delete File Extensions
119     'First do the master service
120     DeleteFileExtensions WebObj,ShibISAPIPath
121     'Now do the websites
122     for each site in WebObj
123       if (site.Class = "IIsWebServer") then
124         set siteObj = GetObject("IIS://LocalHost/W3SVC/" & site.Name & "/ROOT")
125         DeleteFileExtensions siteObj,ShibISAPIPath
126       end if
127     next
128
129     'Delete Web Services Extension (universal, no need to do for each site)
130     WebObj.DeleteExtensionFileRecord ShibISAPIPath
131   ' Got the IIS Object
132   End If
133 ' Sense whether this is an upgrade
134 end if