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