new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / win32 / ossec-installer.nsi
1 ; include Modern UI
2 !include "MUI.nsh"
3
4 ; standard NSIS includes
5 !include "LogicLib.nsh"
6 !include "WinVer.nsh"
7
8 ; include nsProcess
9 !addincludedir "nsProcess"
10 !addplugindir "nsProcess"
11 !include "nsProcess.nsh"
12
13 ; include SimpleSC
14 !addplugindir "SimpleSC"
15
16 ; include GetTime
17 !include "FileFunc.nsh"
18 !insertmacro GetTime
19
20 ; output file
21 !ifndef OutFile
22     !define OutFile "ossec-win32-agent.exe"
23 !endif
24
25 ; general
26 !define MUI_ICON favicon.ico
27 !define MUI_UNICON ossec-uninstall.ico
28 !define VERSION "3.3.0"
29 !define NAME "OSSEC HIDS"
30 !define SERVICE "OssecSvc"
31
32 Name "${NAME} Windows Agent v${VERSION}"
33 BrandingText "Copyright (C) 2003 - 2014 Trend Micro Inc."
34 OutFile "${OutFile}"
35
36 InstallDir "$PROGRAMFILES\ossec-agent"
37 InstallDirRegKey HKLM Software\OSSEC ""
38
39 ; show (un)installation details
40 ShowInstDetails show
41 ShowUninstDetails show
42
43 ; do not close details pages immediately
44 !define MUI_FINISHPAGE_NOAUTOCLOSE
45 !define MUI_UNFINISHPAGE_NOAUTOCLOSE
46
47 ; interface settings
48 !define MUI_ABORTWARNING
49
50 ; pages
51 !define MUI_WELCOMEPAGE_TITLE_3LINES
52 !define MUI_WELCOMEPAGE_TEXT "This wizard will guide you through the install of ${Name}.\r\n\r\nClick next to continue."
53 !define MUI_FINISHPAGE_TITLE_3LINES
54 !define MUI_FINISHPAGE_RUN "$INSTDIR\win32ui.exe"
55 !define MUI_FINISHPAGE_RUN_TEXT "Run OSSEC Agent Manager"
56
57 ; page for choosing components
58 !define MUI_COMPONENTSPAGE_TEXT_TOP "Select the options you want to be executed. Click next to continue."
59 !define MUI_COMPONENTSPAGE_NODESC
60
61 ; pages to display to user
62 !insertmacro MUI_PAGE_WELCOME
63 !insertmacro MUI_PAGE_LICENSE "LICENSE.txt"
64 !insertmacro MUI_PAGE_COMPONENTS
65 !insertmacro MUI_PAGE_DIRECTORY
66 !insertmacro MUI_PAGE_INSTFILES
67 !insertmacro MUI_PAGE_FINISH
68
69 ; these have to be defined again to work with the uninstall pages
70 !define MUI_WELCOMEPAGE_TITLE_3LINES
71 !define MUI_FINISHPAGE_TITLE_3LINES
72 !insertmacro MUI_UNPAGE_WELCOME
73 !insertmacro MUI_UNPAGE_CONFIRM
74 !insertmacro MUI_UNPAGE_INSTFILES
75 !insertmacro MUI_UNPAGE_FINISH
76
77 ; languages
78 !insertmacro MUI_LANGUAGE "English"
79
80 ; function to stop OSSEC service if running
81 Function .onInit
82     ; stop service
83     SimpleSC::ExistsService "${SERVICE}"
84     Pop $0
85     ${If} $0 = 0
86         SimpleSC::ServiceIsStopped "${SERVICE}"
87         Pop $0
88         Pop $1
89         ${If} $0 = 0
90             ${If} $1 <> 1
91                 MessageBox MB_OKCANCEL "${NAME} is already installed and the ${SERVICE} service is running. \
92                     It will be stopped before continuing." /SD IDOK IDOK ServiceStop
93                 SetErrorLevel 2
94                 Abort
95
96                 ServiceStop:
97                     SimpleSC::StopService "${SERVICE}" 1 30
98                     Pop $0
99                     ${If} $0 <> 0
100                         MessageBox MB_ABORTRETRYIGNORE|MB_ICONSTOP "$\r$\n\
101                             Failure stopping the ${SERVICE} service ($0).$\r$\n$\r$\n\
102                             Click Abort to stop the installation,$\r$\n\
103                             Retry to try again, or$\r$\n\
104                             Ignore to skip this file." /SD IDABORT IDIGNORE ServiceStopped IDRETRY ServiceStop
105
106                         SetErrorLevel 2
107                         Abort
108                     ${EndIf}
109             ${EndIf}
110         ${Else}
111             MessageBox MB_ABORTRETRYIGNORE|MB_ICONSTOP "$\r$\n\
112                 Failure checking status of the ${SERVICE} service ($0).$\r$\n$\r$\n\
113                 Click Abort to stop the installation,$\r$\n\
114                 Retry to try again, or$\r$\n\
115                 Ignore to skip this file." /SD IDABORT IDIGNORE ServiceStopped IDRETRY ServiceStop
116
117             SetErrorLevel 2
118             Abort
119         ${EndIf}
120     ${EndIf}
121     ServiceStopped:
122 FunctionEnd
123
124 ; main install section
125 Section "OSSEC Agent (required)" MainSec
126     ; set install type and cwd
127     SectionIn RO
128     SetOutPath $INSTDIR
129
130     ; clear any errors
131     ClearErrors
132
133     ; use real date modified times
134     SetDateSave off
135
136     ; overwrite existing files
137     SetOverwrite on
138
139     ; create necessary directories
140     CreateDirectory "$INSTDIR\bookmarks"
141     CreateDirectory "$INSTDIR\rids"
142     CreateDirectory "$INSTDIR\syscheck"
143     CreateDirectory "$INSTDIR\shared"
144     CreateDirectory "$INSTDIR\active-response"
145     CreateDirectory "$INSTDIR\active-response\bin"
146     CreateDirectory "$INSTDIR\tmp"
147
148     ; install files
149     ;File ossec-lua.exe
150     ;File ossec-luac.exe
151     File ossec-agent.exe
152     File ossec-agent-eventchannel.exe
153     File agent-auth.exe
154     File default-ossec.conf
155     File manage_agents.exe
156     File /oname=win32ui.exe os_win32ui.exe
157     File ossec-rootcheck.exe
158     File internal_options.conf
159     File default-local_internal_options.conf
160     File setup-windows.exe
161     File setup-syscheck.exe
162     File setup-iis.exe
163     File doc.html
164     File /oname=shared\rootkit_trojans.txt ../rootcheck/db/rootkit_trojans.txt
165     File /oname=shared\rootkit_files.txt ../rootcheck/db/rootkit_files.txt
166     File add-localfile.exe
167     File LICENSE.txt
168     File /oname=shared\win_applications_rcl.txt ../rootcheck\db\win_applications_rcl.txt
169     File /oname=shared\win_malware_rcl.txt ../rootcheck\db\win_malware_rcl.txt
170     File /oname=shared\win_audit_rcl.txt ../rootcheck\db\win_audit_rcl.txt
171     File help.txt
172     File vista_sec.txt
173     File /oname=active-response\bin\route-null.cmd route-null.cmd
174     File /oname=active-response\bin\restart-ossec.cmd restart-ossec.cmd
175
176     ; use appropriate version of "ossec-agent.exe"
177     ${If} ${AtLeastWinVista}
178         Delete "$INSTDIR\ossec-agent.exe"
179         Rename "$INSTDIR\ossec-agent-eventchannel.exe" "$INSTDIR\ossec-agent.exe"
180     ${Else}
181         Delete "$INSTDIR\ossec-agent-eventchannel.exe"
182     ${Endif}
183
184     ; write registry keys
185     WriteRegStr HKLM SOFTWARE\ossec "Install_Dir" "$INSTDIR"
186     WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\OSSEC" "DisplayName" "${NAME} ${VERSION}"
187     WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\OSSEC" "DisplayVersion" "${VERSION}"
188     WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\OSSEC" "DisplayIcon" "${MUI_ICON}"
189     WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\OSSEC" "HelpLink" "http://www.ossec.net/main/support/"
190     WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\OSSEC" "URLInfoAbout" "http://www.ossec.net"
191     WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ossec" "UninstallString" '"$INSTDIR\uninstall.exe"'
192     WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ossec" "NoModify" 1
193     WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ossec" "NoRepair" 1
194     WriteUninstaller "uninstall.exe"
195
196     ; get current local time
197     ${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6
198     var /global CURRENTTIME
199     StrCpy $CURRENTTIME "$2-$1-$0 $4:$5:$6"
200
201     ; write version and install information
202     VersionInstall:
203         FileOpen $0 "$INSTDIR\VERSION.txt" w
204         FileWrite $0 "${NAME} v${VERSION} - Installed on $CURRENTTIME"
205         FileClose $0
206         IfErrors VersionError VersionComplete
207     VersionError:
208         MessageBox MB_ABORTRETRYIGNORE|MB_ICONSTOP "$\r$\n\
209             Failure saving version to file.$\r$\n$\r$\n\
210             File:$\r$\n$\r$\n$INSTDIR\VERSION.txt$\r$\n$\r$\n\
211             Click Abort to stop the installation,$\r$\n\
212             Retry to try again, or$\r$\n\
213             Ignore to skip this file." /SD IDABORT IDIGNORE VersionComplete IDRETRY VersionInstall
214
215         SetErrorLevel 2
216         Abort
217     VersionComplete:
218         ClearErrors
219
220     ; create log file
221     LogInstall:
222         ClearErrors
223         IfFileExists "$INSTDIR\ossec.log" LogComplete
224         FileOpen $0 "$INSTDIR\ossec.log" w
225         FileClose $0
226         IfErrors LogError LogComplete
227     LogError:
228         MessageBox MB_ABORTRETRYIGNORE|MB_ICONSTOP "$\r$\n\
229             Failure creating the ossec.log file.$\r$\n$\r$\n\
230             File:$\r$\n$\r$\n$INSTDIR\ossec.log$\r$\n$\r$\n\
231             Click Abort to stop the installation,$\r$\n\
232             Retry to try again, or$\r$\n\
233             Ignore to skip this file." /SD IDABORT IDIGNORE LogComplete IDRETRY LogInstall
234
235         SetErrorLevel 2
236         Abort
237     LogComplete:
238         ClearErrors
239
240     ; rename local_internal_options.conf if it does not already exist
241     ConfInstallInternal:
242         ClearErrors
243         IfFileExists "$INSTDIR\local_internal_options.conf" ConfPresentInternal
244         Rename "$INSTDIR\default-local_internal_options.conf" "$INSTDIR\local_internal_options.conf"
245         IfErrors ConfErrorInternal ConfPresentInternal
246     ConfErrorInternal:
247         MessageBox MB_ABORTRETRYIGNORE|MB_ICONSTOP "$\r$\n\
248             Failure renaming configuration file.$\r$\n$\r$\n\
249             From:$\r$\n$\r$\n\
250             $INSTDIR\default-local_internal_options.conf$\r$\n$\r$\n\
251             To:$\r$\n$\r$\n\
252             $INSTDIR\local_internal_options.conf$\r$\n$\r$\n\
253             Click Abort to stop the installation,$\r$\n\
254             Retry to try again, or$\r$\n\
255             Ignore to skip this file." /SD IDABORT IDIGNORE ConfPresentInternal IDRETRY ConfInstallInternal
256
257         SetErrorLevel 2
258         Abort
259     ConfPresentInternal:
260         ClearErrors
261
262     ; rename ossec.conf if it does not already exist
263     ConfInstallOSSEC:
264         ClearErrors
265         IfFileExists "$INSTDIR\ossec.conf" ConfPresentOSSEC
266         Rename "$INSTDIR\default-ossec.conf" "$INSTDIR\ossec.conf"
267         IfErrors ConfErrorOSSEC ConfPresentOSSEC
268     ConfErrorOSSEC:
269         MessageBox MB_ABORTRETRYIGNORE|MB_ICONSTOP "$\r$\n\
270             Failure renaming configuration file.$\r$\n$\r$\n\
271             From:$\r$\n$\r$\n\
272             $INSTDIR\default-ossec.conf$\r$\n$\r$\n\
273             To:$\r$\n$\r$\n\
274             $INSTDIR\ossec.conf$\r$\n$\r$\n\
275             Click Abort to stop the installation,$\r$\n\
276             Retry to try again, or$\r$\n\
277             Ignore to skip this file." /SD IDABORT IDIGNORE ConfPresentOSSEC IDRETRY ConfInstallOSSEC
278
279         SetErrorLevel 2
280         Abort
281     ConfPresentOSSEC:
282         ClearErrors
283
284     ; handle shortcuts
285     ; http://nsis.sourceforge.net/Shortcuts_removal_fails_on_Windows_Vista
286     SetShellVarContext all
287
288     ; remove shortcuts
289     Delete "$SMPROGRAMS\OSSEC\Edit.lnk"
290     Delete "$SMPROGRAMS\OSSEC\Uninstall.lnk"
291     Delete "$SMPROGRAMS\OSSEC\Documentation.lnk"
292     Delete "$SMPROGRAMS\OSSEC\Edit Config.lnk"
293     Delete "$SMPROGRAMS\OSSEC\*.*"
294     RMDir "$SMPROGRAMS\OSSEC"
295
296     ; create shortcuts
297     CreateDirectory "$SMPROGRAMS\OSSEC"
298     CreateShortCut "$SMPROGRAMS\OSSEC\Manage Agent.lnk" "$INSTDIR\win32ui.exe" "" "$INSTDIR\win32ui.exe" 0
299     CreateShortCut "$SMPROGRAMS\OSSEC\Documentation.lnk" "$INSTDIR\doc.html" "" "$INSTDIR\doc.html" 0
300     CreateShortCut "$SMPROGRAMS\OSSEC\Edit Config.lnk" "$INSTDIR\ossec.conf" "" "$INSTDIR\ossec.conf" 0
301     CreateShortCut "$SMPROGRAMS\OSSEC\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
302
303     ; install OSSEC service
304     ServiceInstall:
305         nsExec::ExecToLog '"$INSTDIR\ossec-agent.exe" install-service'
306         Pop $0
307         ${If} $0 <> 1
308             MessageBox MB_ABORTRETRYIGNORE|MB_ICONSTOP "$\r$\n\
309                 Failure setting up the ${SERVICE} service.$\r$\n$\r$\n\
310                 Check the details for information about the error.$\r$\n$\r$\n\
311                 Click Abort to stop the installation,$\r$\n\
312                 Retry to try again, or$\r$\n\
313                 Ignore to skip this file." /SD IDABORT IDIGNORE ServiceInstallComplete IDRETRY ServiceInstall
314
315             SetErrorLevel 2
316             Abort
317         ${EndIf}
318     ServiceInstallComplete:
319
320     ; install files
321     Setup:
322         nsExec::ExecToLog '"$INSTDIR\setup-windows.exe" "$INSTDIR"'
323         Pop $0
324         ${If} $0 <> 1
325             MessageBox MB_ABORTRETRYIGNORE|MB_ICONSTOP "$\r$\n\
326                 Failure running setup-windows.exe.$\r$\n$\r$\n\
327                 Check the details for information about the error.$\r$\n$\r$\n\
328                 Click Abort to stop the installation,$\r$\n\
329                 Retry to try again, or$\r$\n\
330                 Ignore to skip this file." /SD IDABORT IDIGNORE SetupComplete IDRETRY Setup
331
332             SetErrorLevel 2
333             Abort
334         ${EndIf}
335     SetupComplete:
336 SectionEnd
337
338 ; add IIS logs
339 Section "Scan and monitor IIS logs (recommended)" IISLogs
340     nsExec::ExecToLog '"$INSTDIR\setup-iis.exe" "$INSTDIR"'
341 SectionEnd
342
343 ; add integrity checking
344 Section "Enable integrity checking (recommended)" IntChecking
345     nsExec::ExecToLog '"$INSTDIR\setup-syscheck.exe" "$INSTDIR" "enable"'
346 SectionEnd
347
348 ; uninstall section
349 Section "Uninstall"
350     ; uninstall the services
351     ; this also stops the service as well so it should be done early
352     ServiceUninstall:
353         nsExec::ExecToLog '"$INSTDIR\ossec-agent.exe" uninstall-service'
354         Pop $0
355         ${If} $0 <> 1
356             MessageBox MB_ABORTRETRYIGNORE|MB_ICONSTOP "$\r$\n\
357                 Failure uninstalling the ${SERVICE} service.$\r$\n$\r$\n\
358                 Check the details for information about the error.$\r$\n$\r$\n\
359                 Click Abort to stop the installation,$\r$\n\
360                 Retry to try again, or$\r$\n\
361                 Ignore to skip this file." /SD IDABORT IDIGNORE ServiceUninstallComplete IDRETRY ServiceUninstall
362
363             SetErrorLevel 2
364             Abort
365         ${EndIf}
366     ServiceUninstallComplete:
367
368     ; make sure manage_agents.exe is not running
369     ManageAgents:
370         ${nsProcess::FindProcess} "manage_agents.exe" $0
371         ${If} $0 = 0
372             MessageBox MB_ABORTRETRYIGNORE|MB_ICONSTOP "$\r$\n\
373                 Found manage_agents.exe is still running.$\r$\n$\r$\n\
374                 Please close it before continuing.$\r$\n$\r$\n\
375                 Click Abort to stop the installation,$\r$\n\
376                 Retry to try again, or$\r$\n\
377                 Ignore to skip this file." /SD IDABORT IDIGNORE ManageAgentsClosed IDRETRY ManageAgents
378
379             ${nsProcess::Unload}
380             SetErrorLevel 2
381             Abort
382         ${EndIf}
383     ManageAgentsClosed:
384
385     ; make sure win32ui.exe is not running
386     win32ui:
387         ${nsProcess::FindProcess} "win32ui.exe" $0
388         ${If} $0 = 0
389             MessageBox MB_ABORTRETRYIGNORE|MB_ICONSTOP "$\r$\n\
390                 Found win32ui.exe is still running.$\r$\n$\r$\n\
391                 Please close it before continuing.$\r$\n$\r$\n\
392                 Click Abort to stop the installation,$\r$\n\
393                 Retry to try again, or$\r$\n\
394                 Ignore to skip this file." /SD IDABORT IDIGNORE win32uiClosed IDRETRY win32ui
395
396             ${nsProcess::Unload}
397             SetErrorLevel 2
398             Abort
399         ${EndIf}
400     win32uiClosed:
401
402     ; unload nsProcess
403     ${nsProcess::Unload}
404
405     ; remove registry keys
406     DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\OSSEC"
407     DeleteRegKey HKLM SOFTWARE\OSSEC
408
409     ; remove files and uninstaller
410     Delete "$INSTDIR\ossec-agent.exe"
411     ;Delete "$INSTDIR\ossec-lua.exe"
412     ;Delete "$INSTDIR\ossec-luac.exe"
413     Delete "$INSTDIR\manage_agents.exe"
414     Delete "$INSTDIR\ossec.conf"
415     Delete "$INSTDIR\uninstall.exe"
416     Delete "$INSTDIR\*"
417     Delete "$INSTDIR\bookmarks\*"
418     Delete "$INSTDIR\rids\*"
419     Delete "$INSTDIR\syscheck\*"
420     Delete "$INSTDIR\shared\*"
421     Delete "$INSTDIR\active-response\bin\*"
422     Delete "$INSTDIR\active-response\*"
423     Delete "$INSTDIR\tmp\*"
424     Delete "$INSTDIR"
425
426     ; remove shortcuts
427     SetShellVarContext all
428     Delete "$SMPROGRAMS\OSSEC\*.*"
429     Delete "$SMPROGRAMS\OSSEC\*"
430     RMDir "$SMPROGRAMS\OSSEC"
431
432     ; remove directories used
433     RMDir "$INSTDIR\shared"
434     RMDir "$INSTDIR\syscheck"
435     RMDir "$INSTDIR\bookmarks"
436     RMDir "$INSTDIR\rids"
437     RMDir "$INSTDIR\active-response\bin"
438     RMDir "$INSTDIR\active-response"
439     RMDir "$INSTDIR\tmp"
440     RMDir "$INSTDIR"
441 SectionEnd