Expose no-startmenu option to post-install and pre-remove scripts
[cygwin-setup.git] / AntiVirus.cc
blobcc416cc7332ab27efb94fe78fa9239c620c1f7f4
1 /*
2 * Copyright (c) 2000, Red Hat, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
12 * Written by DJ Delorie <dj@cygnus.com>
16 #include "AntiVirus.h"
18 #include "getopt++/BoolOption.h"
20 #include "LogSingleton.h"
22 #include "win32.h"
23 #include <stdio.h>
24 #include "dialog.h"
25 #include "resource.h"
26 #include "state.h"
27 #include "msg.h"
28 #include "package_db.h"
31 /* XXX: Split this into observer and model classes */
33 /* Default is to leave well enough alone */
34 static BoolOption DisableVirusOption (false, 'A', "disable-buggy-antivirus", IDS_HELPTEXT_DISABLE_ANTIVIRUS);
36 static bool KnownAVIsPresent = false;
37 static bool AVRunning = true;
38 static SC_HANDLE SCM = NULL;
39 static SC_HANDLE McAfeeService = NULL;
40 static void detect();
42 static int rb[] =
43 { IDC_DISABLE_AV, IDC_LEAVE_AV, 0};
45 static int disableAV = IDC_LEAVE_AV;
47 static void
48 load_dialog (HWND h)
50 rbset (h, rb, disableAV);
53 static void
54 save_dialog (HWND h)
56 disableAV = rbget (h, rb);
59 static BOOL
60 dialog_cmd (HWND h, int id, HWND hwndctl, UINT code)
62 switch (id)
65 case IDC_DISABLE_AV:
66 case IDC_LEAVE_AV:
67 save_dialog (h);
68 break;
70 default:
71 break;
73 return 0;
76 bool
77 AntiVirusPage::Create ()
79 detect();
80 return PropertyPage::Create (NULL, dialog_cmd, IDD_VIRUS);
83 void
84 AntiVirusPage::OnActivate ()
86 load_dialog (GetHWND ());
87 // Check to see if any radio buttons are selected. If not, select a default.
88 if (SendMessage (GetDlgItem (IDC_DISABLE_AV), BM_GETCHECK, 0, 0)
89 != BST_CHECKED
90 && SendMessage (GetDlgItem (IDC_LEAVE_AV), BM_GETCHECK, 0, 0)
91 != BST_CHECKED)
92 SendMessage (GetDlgItem (IDC_LEAVE_AV), BM_SETCHECK, BST_CHECKED, 0);
95 bool
96 AntiVirusPage::wantsActivation() const
98 // Check if there's an antivirus scanner to be disabled.
99 if(!KnownAVIsPresent)
101 // Nope, skip this page by "not accepting" activation.
102 return false;
105 return true;
108 long
109 AntiVirusPage::OnNext ()
111 HWND h = GetHWND ();
113 save_dialog (h);
114 /* if disable, do so now */
115 return 0;
118 long
119 AntiVirusPage::OnBack ()
121 save_dialog (GetHWND ());
122 return 0;
125 void
126 AntiVirusPage::OnDeactivate ()
128 if (!KnownAVIsPresent)
129 return;
130 if (disableAV == IDC_LEAVE_AV)
131 return;
133 SERVICE_STATUS status;
134 if (!ControlService (McAfeeService, SERVICE_CONTROL_STOP, &status) &&
135 GetLastError() != ERROR_SERVICE_NOT_ACTIVE)
137 Log (LOG_PLAIN) << "Could not stop McAfee service, disabled AV logic"
138 << endLog;
139 disableAV = IDC_LEAVE_AV;
140 return;
143 AVRunning = false;
144 Log (LOG_PLAIN) << "Disabled Anti Virus software" << endLog;
147 long
148 AntiVirusPage::OnUnattended ()
150 if (!KnownAVIsPresent)
151 return OnNext();
152 if ((bool)DisableVirusOption)
153 disableAV = IDC_DISABLE_AV;
154 else
155 disableAV = IDC_LEAVE_AV;
156 return OnNext();
159 void
160 detect ()
162 // TODO: trim the access rights down
163 SCM = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);
165 if (!SCM) {
166 Log (LOG_BABBLE) << "Could not open Service control manager" << endLog;
167 return;
170 /* in future, factor this to a routine to find service foo (ie norton, older
171 mcafee etc
173 McAfeeService = OpenService (SCM, "AvSynMgr",
174 SERVICE_QUERY_STATUS| SERVICE_STOP| SERVICE_START);
176 if (!McAfeeService) {
177 Log (LOG_BABBLE) << "Could not open service McShield for query, start and stop. McAfee may not be installed, or we don't have access." << endLog;
178 CloseServiceHandle(SCM);
179 return;
182 SERVICE_STATUS status;
184 if (!QueryServiceStatus (McAfeeService, &status))
186 CloseServiceHandle(SCM);
187 CloseServiceHandle(McAfeeService);
188 Log (LOG_PLAIN) << "Couldn't determine status of McAfee service."
189 << endLog;
190 return;
193 if (status.dwCurrentState == SERVICE_STOPPED ||
194 status.dwCurrentState == SERVICE_STOP_PENDING)
196 CloseServiceHandle(SCM);
197 CloseServiceHandle(McAfeeService);
198 Log (LOG_PLAIN) << "Mcafee is already stopped, nothing to see here"
199 << endLog;
202 Log (LOG_PLAIN) << "Found McAfee anti virus program" << endLog;
203 KnownAVIsPresent = true;
206 bool
207 AntiVirus::Show()
209 return KnownAVIsPresent;
212 void
213 AntiVirus::AtExit()
215 if (!KnownAVIsPresent)
216 return;
217 if (disableAV == IDC_LEAVE_AV)
218 return;
219 if (AVRunning == true)
220 return;
222 if (!StartService(McAfeeService, 0, NULL))
224 Log (LOG_PLAIN) << "Could not start McAfee service again, disabled AV logic" << endLog;
225 disableAV = IDC_LEAVE_AV;
226 return;
229 Log (LOG_PLAIN) << "Enabled Anti Virus software" << endLog;
231 AVRunning = true;