https://issues.shibboleth.net/jira/browse/SSPCPP-447
[shibboleth/cpp-sp.git] / msi / scripts / shib_uninstall_old_versions.vbs
1 'In order to get the list of versions to uninstall during deferred mode,
2 'We need to set UninstallOldShibVersions property during the Immediate
3 'Execution sequence.  We can then read the value via CustomActionData.
4 'To accomplish this, create a CA as follows:
5 '  Action: SetShibVersionsImmediate
6 '  Source: UninstallOldShibVersions
7 '  Type:   51
8 '  Target: [OLDSHIBVERSIONSFOUND]
9 'Sequence this action near the beginning of InstallExecuteSequence with
10 '  Condition: (NOT Installed) AND (OLDSHIBVERSIONSFOUND <> "") AND (OLDSHIBPERFORMUNINSTALL = "TRUE")
11
12
13 '*********************************
14 '* This code is the entire body of shib_uninstall_isapi_filter.vbs
15 '* The only exception is that the main function of that code is made
16 '* a Sub in this code so we can call it, and we pass it the InstallDir
17 '* from the Uninst.isu string instead of pulling it from the MSI.
18 '*********************************
19
20 Sub DeleteISAPIFilters(IISPath,dllPath)
21
22   Dim filter, FiltersObj, LoadOrder, FilterArray, FilterItem
23
24   Set FiltersObj = GetObject(IISPath & "/Filters")
25   LoadOrder = FiltersObj.FilterLoadOrder
26
27   for each filter in FiltersObj
28     if (filter.Class = "IIsFilter") then
29       if (filter.FilterPath = dllPath) then
30
31         'Delete the found filter here
32         'If there's anything to potentially delete...
33         if (LoadOrder <> "") then
34           FilterArray = split(LoadOrder,",")
35           LoadOrder = ""
36           for each FilterItem in FilterArray
37             if (FilterItem <> filter.Name) then
38               LoadOrder = LoadOrder & FilterItem & ","
39             end if
40           next
41           'Remove trailing comma if any filters were kept
42           if (LoadOrder <> "") then
43             LoadOrder = mid(LoadOrder,1,len(LoadOrder)-1)
44           end if
45
46           'Set the Load Order to the new shibboleth-less order
47           if (FiltersObj.FilterLoadOrder <> LoadOrder) then
48             FiltersObj.FilterLoadOrder = LoadOrder
49             FiltersObj.SetInfo
50           end if
51         end if
52
53         'Delete the actual IISFilter object
54         FiltersObj.Delete "IIsFilter",filter.Name
55
56       end if
57     end if
58   next
59
60 End Sub
61
62
63 Sub DeleteFileExtensions(siteObj, dllPath)
64
65 Dim ScriptMaps, newScriptMaps
66 Dim line, lineArray, lineIndex
67 Dim fileExtension
68 Dim existsFlag
69
70   ScriptMaps = siteObj.ScriptMaps
71   redim newScriptMaps(0)
72   lineIndex = 0
73   'copy each entry from the old ScriptMaps to newScriptMaps
74   'unless it is for dllPath
75   for each line in ScriptMaps
76     lineArray = split(line,",")
77     if (lineArray(1) <> dllPath) then
78       redim preserve newScriptMaps(lineIndex)
79       newScriptMaps(lineIndex) = line
80       lineIndex = lineIndex + 1
81     else
82       existsFlag = "exists"
83     end if
84   next
85   'If we found dllPath, then use the newScriptMaps instead
86   if (existsFlag = "exists") then
87     siteObj.ScriptMaps = newScriptMaps
88     siteObj.SetInfo
89   end if
90
91 End Sub
92
93
94 Sub CleanUpISAPI(InstallDir)
95
96 Dim WebObj
97 'Dim InstallDir
98 Dim ShibISAPIPath
99 Dim site, siteObj, sitePath
100
101
102 'Don't show errors, we'll handle anything important
103 On Error Resume Next
104
105 'Attempt to get W3SVC.  If failure, end script (e.g. IIS isn't available)
106 Set WebObj = GetObject("IIS://LocalHost/W3SVC")
107 if (Err = 0) then
108
109   'Get the INSTALLDIR value via CustomActionData
110   'Commented out for embedding in this .vbs, passed instead
111 '  InstallDir = Session.Property("CustomActionData")
112
113   'Remove all trailing backslashes to normalize
114   do while (mid(InstallDir,Len(InstallDir),1) = "\")
115     InstallDir = mid(InstallDir,1,Len(InstallDir)-1)
116   loop
117   'Set dll Path
118   ShibISAPIPath = InstallDir & "\libexec\isapi_shib.dll"
119
120   'Delete ISAPI Filter
121   'First do the master service
122   DeleteISAPIFilters "IIS://LocalHost/W3SVC",ShibISAPIPath
123   'Now do the websites
124   for each site in WebObj
125     if (site.Class = "IIsWebServer") then
126       sitePath = "IIS://LocalHost/W3SVC/" & site.Name
127       DeleteISAPIFilters sitePath,ShibISAPIPath
128     end if
129   next
130
131   'Delete File Extensions
132   'First do the master service
133   DeleteFileExtensions WebObj,ShibISAPIPath
134   'Now do the websites
135   for each site in WebObj
136     if (site.Class = "IIsWebServer") then
137       set siteObj = GetObject("IIS://LocalHost/W3SVC/" & site.Name & "/ROOT")
138       DeleteFileExtensions siteObj,ShibISAPIPath
139     end if
140   next
141
142
143   'Delete Web Services Extension (universal, no need to do for each site)
144   WebObj.DeleteExtensionFileRecord ShibISAPIPath
145
146 'Last end if
147 end if
148
149 End Sub
150
151
152 '******** Begin Main Code ***************
153
154 Dim WshShell, WshEnv, versionArray, versionElement, versionNumbers, regValue, UninstallArray, uninstallStr, UninstIsuArray, path, pathArray, NewPathEnv
155
156 on error resume next
157 Set WshShell = CreateObject("WScript.Shell")
158
159 versionNumbers = Session.Property("CustomActionData")
160
161 versionArray = split( versionNumbers, vbCRLF )
162
163 for each versionElement in versionArray
164   if (versionElement<>"") then
165
166     'if RegRead fails, it won't set regValue, and it will hold the last value instead.  Make sure the 'last' value is ""
167     regValue = ""
168     on error resume next
169       regValue=WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & versionElement & "\UninstallString")
170     if (regValue<>"") then
171       UninstallArray = split( regValue, " -f")
172       'Save off the INSTALLDIR path for later use
173       UninstIsuArray = split(UninstallArray(1),"Uninst.isu")
174       InstallDir = UninstIsuArray(0)
175
176       'Now create the silent uninstall string and execute it
177       uninstallStr=UninstallArray(0) & " -y -a -f" & UninstallArray(1)
178       WshShell.Run( uninstallStr )
179
180       'Remove entry from path environment variable
181       Set WshEnv = WshShell.Environment("SYSTEM")
182       PathEnv = WshEnv("PATH")
183       NewPathEnv = ""
184       PathArray = split(PathEnv,";")
185       for each path in PathArray
186         if ((path<>InstallDir & "lib\") AND (path<>InstallDir & "lib")) then
187           NewPathEnv = NewPathEnv & path & ";"
188         end if
189       next
190       NewPathEnv = mid(NewPathEnv,1,len(NewPathEnv)-1)
191       WshEnv("PATH") = NewPathEnv
192
193       'Clean up all the ISAPI filters and file extension
194       CleanUpISAPI InstallDir
195
196     end if
197
198   end if
199 next