"SfR Fresh" - the SfR Freeware/Shareware Archive 
Member "nmap-5.21/mswin32/winpcap/winpcap-nmap.nsi" of archive nmap-5.21.tgz:
As a special service "SfR Fresh" has tried to format the requested source page into HTML format using source code syntax highlighting with prefixed line numbers.
Alternatively you can here view or download the uninterpreted source code file.
That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
1 ;; Custom winpcap for nmap
2 ;; Recognizes the options (case sensistive):
3 ;; /S silent install
4 ;; /NPFSTARTUP=NO start NPF now and at startup (only has effect with /S)
5
6 ;; Started by Doug Hoyte, April 2006
7
8 ;; Eddie Bell
9 ;; Updated to 4.0, June 2007
10 ;; Updated to 4.01, July 2007
11 ;; Updated to 4.02, November 2007
12
13 ;; Rob Nicholls
14 ;; Updated to 4.1.1, October 2009
15
16 ;--------------------------------
17 ;Include Modern UI
18
19 !include "MUI.nsh"
20 !include "FileFunc.nsh"
21
22 ;--------------------------------
23 ;General
24
25 ; The name of the installer
26 Name "WinPcap (Nmap) 4.1.1"
27
28 ; The file to write
29 OutFile "winpcap-nmap-4.11.exe"
30
31 RequestExecutionLevel admin
32
33 ; The default installation directory
34 InstallDir $PROGRAMFILES\WinPcap
35 ;No longer check registry for the location, let installer handle everything.
36
37 VIProductVersion "4.1.0.1753"
38 VIAddVersionKey /LANG=1033 "FileVersion" "4.1.0.1753"
39 VIAddVersionKey /LANG=1033 "ProductName" "WinPcap"
40 VIAddVersionKey /LANG=1033 "FileDescription" "WinPcap 4.1.1 installer"
41 VIAddVersionKey /LANG=1033 "LegalCopyright" ""
42
43 ;--------------------------------
44 ; Windows API Definitions
45
46 !define SC_MANAGER_ALL_ACCESS 0x3F
47 !define SERVICE_ALL_ACCESS 0xF01FF
48
49 ; Service Types
50 !define SERVICE_FILE_SYSTEM_DRIVER 0x00000002
51 !define SERVICE_KERNEL_DRIVER 0x00000001
52 !define SERVICE_WIN32_OWN_PROCESS 0x00000010
53 !define SERVICE_WIN32_SHARE_PROCESS 0x00000020
54 !define SERVICE_INTERACTIVE_PROCESS 0x00000100
55
56 ; Service start options
57 !define SERVICE_AUTO_START 0x00000002
58 !define SERVICE_BOOT_START 0x00000000
59 !define SERVICE_DEMAND_START 0x00000003
60 !define SERVICE_DISABLED 0x00000004
61 !define SERVICE_SYSTEM_START 0x00000001
62
63 ; Service Error control
64 !define SERVICE_ERROR_CRITICAL 0x00000003
65 !define SERVICE_ERROR_IGNORE 0x00000000
66 !define SERVICE_ERROR_NORMAL 0x00000001
67 !define SERVICE_ERROR_SEVERE 0x00000002
68
69 ; Service Control Options
70 !define SERVICE_CONTROL_STOP 0x00000001
71 !define SERVICE_CONTROL_PAUSE 0x00000002
72
73
74
75 ;--------------------------------
76 ;Interface Settings
77
78 !define MUI_ABORTWARNING
79
80 ;--------------------------------
81 ;Pages
82
83 !insertmacro MUI_PAGE_LICENSE "LICENSE"
84 ; Don't let user choose where to install the files. WinPcap doesn't let people, and it's one less thing for us to worry about.
85 !insertmacro MUI_PAGE_INSTFILES
86 !insertmacro MUI_UNPAGE_CONFIRM
87 !insertmacro MUI_UNPAGE_INSTFILES
88 Page custom optionsPage doOptions
89 Page custom finalPage doFinal
90
91 ;--------------------------------
92 ;Languages
93
94 !insertmacro MUI_LANGUAGE "English"
95
96 ;--------------------------------
97 ;Reserves
98
99 ReserveFile "options.ini"
100 ReserveFile "final.ini"
101 !insertmacro MUI_RESERVEFILE_INSTALLOPTIONS
102
103 ;--------------------------------
104
105 !insertmacro GetParameters
106 !insertmacro GetOptions
107
108 ; This function is called on startup. IfSilent checks
109 ; if the flag /S was specified. If so, it sets the installer
110 ; to run in "silent mode" which displays no windows and accepts
111 ; all defaults.
112
113 ; We also check if there is a previously installed winpcap
114 ; on this system. If it's the same as the version we're installing,
115 ; abort the install. If not, prompt the user about whether to
116 ; replace it or not.
117
118 Function .onInit
119 !insertmacro MUI_INSTALLOPTIONS_EXTRACT "options.ini"
120 !insertmacro MUI_INSTALLOPTIONS_EXTRACT "final.ini"
121
122 var /GLOBAL inst_ver
123 var /GLOBAL my_ver
124 var /GLOBAL npf_startup
125 StrCpy $my_ver "4.1.0.1753"
126 StrCpy $npf_startup "YES"
127
128 ${GetParameters} $R0
129 ClearErrors
130 ${GetOptions} $R0 "/NPFSTARTUP=" $npf_startup
131
132 IfSilent do_silent no_silent
133
134 do_silent:
135 SetSilent silent
136 IfFileExists "$SYSDIR\wpcap.dll" silent_checks
137 return
138 silent_checks:
139 ; check for the presence of Nmap's custom WinPcapInst registry key:
140 ReadRegStr $0 "HKLM" "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "InstalledBy"
141 StrCmp $0 "Nmap" silent_uninstall winpcap_installedby_keys_not_present
142
143 winpcap_installedby_keys_not_present:
144 ; check for the presence of WinPcapInst's UninstallString
145 ; and manually cleanup registry entries to avoid running
146 ; the GUI uninstaller and assume our installer will overwrite
147 ; the files. Needs to be checked in case someone (force)
148 ; installs WinPcap over the top of our installation
149 ReadRegStr $0 "HKLM" "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "UninstallString"
150 StrCmp $0 "" winpcap_keys_not_present
151
152 DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst"
153
154 ReadRegStr $0 "HKLM" "Software\WinPcap" ""
155 StrCmp $0 "" winpcap_keys_not_present
156
157 Delete $0\rpcapd.exe
158 Delete $0\LICENSE
159 Delete $0\uninstall.exe
160 ; Official 4.1 installer creates an install.log
161 Delete $0\install.log
162 RMDir "$0"
163 DeleteRegKey HKLM "Software\WinPcap"
164
165 ; because we've deleted their uninstaller, skip the next
166 ; registry key check (we'll still need to overwrite stuff)
167 Goto winpcap-nmap_keys_not_present
168
169 winpcap_keys_not_present:
170
171 ; if our old registry key is present then assume all is well
172 ; (we got this far so the official WinPcap wasn't installed)
173 ; and use our uninstaller to (magically) silently uninstall
174 ; everything cleanly and avoid having to overwrite files
175 ReadRegStr $0 "HKLM" "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\winpcap-nmap" "UninstallString"
176 StrCmp $0 "" winpcap-nmap_keys_not_present silent_uninstall
177
178 winpcap-nmap_keys_not_present:
179
180 ; setoverwrite on to try and avoid any problems when trying to install the files
181 ; wpcap.dll is still present at this point, but unclear where it came from
182 SetOverwrite on
183
184 ; try to ensure that npf has been stopped before we install/overwrite files
185 ExecWait '"net stop npf"'
186
187 return
188
189 silent_uninstall:
190 ; Our old UninstallString is present, should have quotes and uninstall.exe location
191 ; and should support a silent uninstall by passing /S to it.
192 ExecWait '$0 /S _?=$INSTDIR'
193 return
194
195 no_silent:
196 IfFileExists "$SYSDIR\wpcap.dll" do_version_check
197 return
198
199 do_version_check:
200
201 GetDllVersion "$SYSDIR\wpcap.dll" $R0 $R1
202 IntOp $R2 $R0 / 0x00010000
203 IntOp $R3 $R0 & 0x0000FFFF
204 IntOp $R4 $R1 / 0x00010000
205 IntOp $R5 $R1 & 0x0000FFFF
206 StrCpy $inst_ver "$R2.$R3.$R4.$R5"
207
208 StrCmp $inst_ver $my_ver same_ver
209
210 MessageBox MB_YESNO|MB_ICONQUESTION "WinPcap version $inst_ver exists on this system. Replace with version $my_ver?" IDYES try_uninstallers
211 quit
212
213 same_ver:
214 MessageBox MB_OK "Skipping WinPcap installation since version $inst_ver already exists on this system. Uninstall that version first if you wish to force install."
215 quit
216
217 try_uninstallers:
218
219 ; check for UninstallString and use that in preference (should already have double quotes and uninstall.exe)
220 ReadRegStr $0 "HKLM" "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "UninstallString"
221 StrCmp $0 "" no_uninstallstring
222 IfFileExists "$0" uninstaller_exists no_uninstallstring
223 uninstaller_exists:
224 ExecWait '$0 _?=$INSTDIR'
225 return
226
227 no_uninstallstring:
228 ; didn't find an UninstallString, check for our old UninstallString and if uninstall.exe exists:
229 ReadRegStr $0 "HKLM" "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\winpcap-nmap" "UninstallString"
230 StrCmp $0 "" still_no_uninstallstring
231 IfFileExists "$0" old_uninstaller_exists still_no_uninstallstring
232 old_uninstaller_exists:
233 MessageBox MB_OK "Using our old UninstallString, file exists"
234 ExecWait '$0 _?=$INSTDIR'
235 return
236
237 still_no_uninstallstring:
238 ; still didn't find anything, try looking for an uninstall.exe file at:
239 ReadRegStr $0 "HKLM" "Software\WinPcap" ""
240 ; Strip any surrounding double quotes from around the install string,
241 ; as WinPcap hasn't used quotes in the past, but our old installers did.
242 ; Check the first and last character for safety!
243 StrCpy $1 $0 1
244 StrCmp $1 "$\"" maybestripquotes nostrip
245 maybestripquotes:
246 StrLen $1 $0
247 IntOp $1 $1 - 1
248 StrCpy $1 $0 1 $1
249 StrCmp $1 "$\"" stripquotes nostrip
250 stripquotes:
251 StrCpy $0 $0 -1 1
252 nostrip:
253 IfFileExists "$0\uninstall.exe" run_last_uninstaller no_uninstall_exe
254 run_last_uninstaller:
255 ExecWait '"$0\Uninstall.exe" _?=$INSTDIR'
256 no_uninstall_exe:
257 ; give up now, we've tried our hardest to determine a valid uninstaller!
258 return
259
260 FunctionEnd
261
262 Function optionsPage
263 !insertmacro MUI_HEADER_TEXT "WinPcap Options" ""
264 !insertmacro MUI_INSTALLOPTIONS_DISPLAY "options.ini"
265 FunctionEnd
266
267 Function doOptions
268 ReadINIStr $0 "$PLUGINSDIR\options.ini" "Field 1" "State"
269 StrCmp $0 "0" do_options_next
270 WriteRegDWORD HKLM "SYSTEM\CurrentControlSet\Services\NPF" "Start" 2
271 do_options_next:
272 ReadINIStr $0 "$PLUGINSDIR\options.ini" "Field 2" "State"
273 StrCmp $0 "0" do_options_end
274 nsExec::Exec "net start npf"
275 do_options_end:
276 FunctionEnd
277
278 Function finalPage
279 ; diplay a page saying everything's finished
280 !insertmacro MUI_HEADER_TEXT "Finished" "Thank you for installing WinPcap"
281 !insertmacro MUI_INSTALLOPTIONS_DISPLAY "final.ini"
282 FunctionEnd
283
284 Function doFinal
285 ; don't need to do anything
286 FunctionEnd
287
288 Function registerServiceAPI
289 ; delete the npf service to avoid an error message later if it already exists
290 System::Call 'advapi32::OpenSCManagerA(,,i ${SC_MANAGER_ALL_ACCESS})i.r0'
291 System::Call 'advapi32::OpenServiceA(i r0,t "npf", i ${SERVICE_ALL_ACCESS}) i.r1'
292 System::Call 'advapi32::DeleteService(i r1) i.r6'
293 System::Call 'advapi32::CloseServiceHandle(i r1) n'
294 System::Call 'advapi32::CloseServiceHandle(i r0) n'
295 ; create the new npf service
296 System::Call 'advapi32::OpenSCManagerA(,,i ${SC_MANAGER_ALL_ACCESS})i.R0'
297 System::Call 'advapi32::CreateServiceA(i R0,t "npf",t "NetGroup Packet Filter Driver",i ${SERVICE_ALL_ACCESS},i ${SERVICE_KERNEL_DRIVER}, i ${SERVICE_DEMAND_START},i ${SERVICE_ERROR_NORMAL}, t "system32\drivers\npf.sys",,,,,) i.r1'
298 StrCmp $1 "0" register_fail register_success
299 register_fail:
300 DetailPrint "Failed to create the npf service"
301 IfSilent close_register_handle register_fail_messagebox
302 register_fail_messagebox:
303 MessageBox MB_OK "Failed to create the npf service. Please try installing WinPcap again, or use the official WinPcap installer from www.winpcap.org"
304 Goto close_register_handle
305 register_success:
306 DetailPrint "The npf service was successfully created"
307 close_register_handle:
308 System::Call 'advapi32::CloseServiceHandle(i R0) n'
309 FunctionEnd
310
311 Function un.registerServiceAPI
312 System::Call 'advapi32::OpenSCManagerA(,,i ${SC_MANAGER_ALL_ACCESS})i.r0'
313 System::Call 'advapi32::OpenServiceA(i r0,t "npf", i ${SERVICE_ALL_ACCESS}) i.r1'
314 System::Call 'advapi32::DeleteService(i r1) i.r6'
315 StrCmp $6 "0" unregister_fail unregister_success
316 unregister_fail:
317 DetailPrint "Failed to delete the npf service"
318 Goto close_unregister_handle
319 unregister_success:
320 DetailPrint "The npf service was successfully deleted"
321 close_unregister_handle:
322 System::Call 'advapi32::CloseServiceHandle(i r1) n'
323 System::Call 'advapi32::CloseServiceHandle(i r0) n'
324 FunctionEnd
325
326 Function autoStartWinPcap
327 WriteRegDWORD HKLM "SYSTEM\CurrentControlSet\Services\NPF" "Start" 2
328 nsExec::Exec "net start npf"
329 FunctionEnd
330
331
332 ;--------------------------------
333 ; The stuff to install
334 Section "WinPcap" SecWinPcap
335
336 ; stop the service, in case it's still registered, so files can be
337 ; safely overwritten and the service can be deleted.
338 nsExec::Exec "net stop npf"
339
340 ; NB: We may need to introduce a check here to ensure that NPF
341 ; has been stopped before we continue, otherwise we Sleep for a
342 ; while and try the check again. This might help prevent any race
343 ; conditions during a silent install (and potentially during the
344 ; slower GUI installation.
345
346 ; These x86 files are automatically redirected to the right place on x64
347 SetOutPath $SYSDIR
348 File pthreadVC.dll
349 File wpcap.dll
350
351 ; Check windows version
352 ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion
353 DetailPrint "Windows CurrentVersion: $R0"
354 StrCpy $R0 $R0 2
355 StrCmp $R0 '6.' vista_files
356
357 File Packet.dll
358 ; WanPacket.dll no longer present as of 4.1.0
359 Goto install
360
361 vista_files:
362 File vista\Packet.dll
363
364 install:
365
366 ; check for x64, install the correct npf.sys file into system32\drivers
367 System::Call "kernel32::GetCurrentProcess() i .s"
368 System::Call "kernel32::IsWow64Process(i s, *i .r0)"
369 StrCmp $0 "0" is32bit is64bit
370
371 ; Note, NSIS states: "You should always quote the path to make sure spaces
372 ; in the path will not disrupt Windows to find the uninstaller."
373 ; See: http://nsis.sourceforge.net/Add_uninstall_information_to_Add/Remove_Programs
374 ; This matches (most) Windows installations. Rather inconsistently,
375 ; DisplayIcon doesn't usually have quotes (even on Microsoft installations) and
376 ; HKLM Software\PackageName doesn't usually have quotes either.
377
378 is32bit:
379 SetOutPath "$PROGRAMFILES\WinPcap"
380 File rpcapd.exe
381 File LICENSE
382 WriteUninstaller "$PROGRAMFILES\WinPcap\uninstall.exe"
383 DetailPrint "Installing x86 driver"
384 SetOutPath $SYSDIR\drivers
385 File npf.sys ; x86 NT5/NT6 version
386 WriteRegStr HKLM "Software\WinPcap" "" "$PROGRAMFILES\WinPcap"
387 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "UninstallString" "$\"$PROGRAMFILES\WinPcap\uninstall.exe$\""
388 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "QuietUninstallString" "$\"$PROGRAMFILES\WinPcap\uninstall.exe$\" /S"
389 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "DisplayIcon" "$PROGRAMFILES\WinPcap\uninstall.exe"
390 Goto npfdone
391
392 is64bit:
393 SetOutPath "$PROGRAMFILES64\WinPcap"
394 File rpcapd.exe
395 File LICENSE
396 WriteUninstaller "$PROGRAMFILES64\WinPcap\uninstall.exe"
397 DetailPrint "Installing x64 driver"
398 SetOutPath $SYSDIR\drivers
399 ; disable Wow64FsRedirection
400 System::Call kernel32::Wow64EnableWow64FsRedirection(i0)
401 File x64\npf.sys ; x64 NT5/NT6 version
402 WriteRegStr HKLM "Software\WinPcap" "" "$PROGRAMFILES64\WinPcap"
403 ; re-enable Wow64FsRedirection
404 System::Call kernel32::Wow64EnableWow64FsRedirection(i1)
405 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "UninstallString" "$\"$PROGRAMFILES64\WinPcap\uninstall.exe$\""
406 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "QuietUninstallString" "$\"$PROGRAMFILES64\WinPcap\uninstall.exe$\" /S"
407 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "DisplayIcon" "$PROGRAMFILES64\WinPcap\uninstall.exe"
408
409 npfdone:
410
411 ; register the driver as a system service using Windows API calls
412 ; this will work on Windows 2000 (that lacks sc.exe) and higher
413 Call registerServiceAPI
414
415 ; Create the default NPF startup setting of 3 (SERVICE_DEMAND_START)
416 WriteRegDWORD HKLM "SYSTEM\CurrentControlSet\Services\NPF" "Start" 3
417
418 ; automatically start the service if performing a silent install, unless
419 ; /NPFSTARTUP=NO was given.
420 IfSilent 0 skip_auto_start
421 StrCmp $npf_startup "NO" skip_auto_start
422 Call autoStartWinPcap
423 skip_auto_start:
424
425 ; Write the rest of the uninstall keys for Windows
426
427 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "DisplayName" "WinPcap 4.1.1"
428 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "DisplayVersion" "4.1.0.1753"
429 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "Publisher" "CACE Technologies"
430 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "URLInfoAbout" "http://www.cacetech.com"
431 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "URLUpdateInfo" "http://www.winpcap.org"
432 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "VersionMajor" "4"
433 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "VersionMinor" "1"
434 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "InstalledBy" "Nmap"
435 WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "NoModify" 1
436 WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst" "NoRepair" 1
437
438 ; delete our legacy winpcap-nmap keys if they still exist (e.g. official 4.0.2 force installed over our 4.0.2):
439 DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\winpcap-nmap"
440
441 SectionEnd ; end the section
442
443
444 ;--------------------------------
445 ;Uninstaller Section
446
447 Section "Uninstall"
448
449 ; stop npf before we delete the service from the registry
450 nsExec::Exec "net stop npf"
451 ; unregister the driver as a system service using Windows API calls, so it works on Windows 2000
452 Call un.registerServiceAPI
453
454 ; delete our winpcap-nmap and any WinPcapInst registry keys
455 DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\winpcap-nmap"
456 DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\WinPcapInst"
457 DeleteRegKey HKLM "Software\WinPcap"
458
459 Delete $INSTDIR\rpcapd.exe
460 Delete $INSTDIR\LICENSE
461 Delete $INSTDIR\uninstall.exe
462
463 Delete $SYSDIR\Packet.dll
464 Delete $SYSDIR\pthreadVC.dll
465 ; No longer need to delete WanPacket.dll as of WinPcap 4.1.0.
466 Delete $SYSDIR\wpcap.dll
467
468 ; check for x64, delete npf.sys file from system32\drivers
469 System::Call "kernel32::GetCurrentProcess() i .s"
470 System::Call "kernel32::IsWow64Process(i s, *i .r0)"
471 StrCmp $0 "0" del32bitnpf del64bitnpf
472 del64bitnpf:
473 ; disable Wow64FsRedirection
474 System::Call kernel32::Wow64EnableWow64FsRedirection(i0)
475
476 Delete $SYSDIR\drivers\npf.sys
477
478 ; re-enable Wow64FsRedirection
479 System::Call kernel32::Wow64EnableWow64FsRedirection(i1)
480 Goto npfdeleted
481 del32bitnpf:
482
483 Delete $SYSDIR\drivers\npf.sys
484
485 npfdeleted:
486
487 RMDir "$INSTDIR"
488
489 SectionEnd