mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0234-v2v-Move-anti-virus-AV-detection-code-to-a-separate-.patch

ffd6ed
From b89803fe63459fbbbfcd7777e293c2be22a9565e Mon Sep 17 00:00:00 2001
ffd6ed
From: "Richard W.M. Jones" <rjones@redhat.com>
ffd6ed
Date: Wed, 9 Sep 2015 13:11:42 +0100
ffd6ed
Subject: [PATCH] v2v: Move anti-virus (AV) detection code to a separate
ffd6ed
 module.
ffd6ed
ffd6ed
This is just code refactoring of earlier
ffd6ed
commit 8e28d6b18860f8ff4e02489317749a723fa145ab.
ffd6ed
ffd6ed
(cherry picked from commit 6734fce8c8a1da083cc259d98e088fe78076dd24)
ffd6ed
---
ffd6ed
 po/POTFILES-ml           |  1 +
ffd6ed
 v2v/Makefile.am          |  3 +++
ffd6ed
 v2v/convert_windows.ml   | 16 ++--------------
ffd6ed
 v2v/detect_antivirus.ml  | 39 +++++++++++++++++++++++++++++++++++++++
ffd6ed
 v2v/detect_antivirus.mli | 23 +++++++++++++++++++++++
ffd6ed
 5 files changed, 68 insertions(+), 14 deletions(-)
ffd6ed
 create mode 100644 v2v/detect_antivirus.ml
ffd6ed
 create mode 100644 v2v/detect_antivirus.mli
ffd6ed
ffd6ed
diff --git a/po/POTFILES-ml b/po/POTFILES-ml
ffd6ed
index 3597d1b..fe177e0 100644
ffd6ed
--- a/po/POTFILES-ml
ffd6ed
+++ b/po/POTFILES-ml
ffd6ed
@@ -87,6 +87,7 @@ v2v/OVF.ml
ffd6ed
 v2v/cmdline.ml
ffd6ed
 v2v/convert_linux.ml
ffd6ed
 v2v/convert_windows.ml
ffd6ed
+v2v/detect_antivirus.ml
ffd6ed
 v2v/domainxml.ml
ffd6ed
 v2v/input_disk.ml
ffd6ed
 v2v/input_libvirt.ml
ffd6ed
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
ffd6ed
index 87e3c3b..77051f6 100644
ffd6ed
--- a/v2v/Makefile.am
ffd6ed
+++ b/v2v/Makefile.am
ffd6ed
@@ -39,6 +39,8 @@ CLEANFILES = *~ *.cmi *.cmo *.cmx *.cmxa *.o virt-v2v
ffd6ed
 SOURCES_MLI = \
ffd6ed
 	convert_linux.mli \
ffd6ed
 	convert_windows.mli \
ffd6ed
+	detect_antivirus.ml \
ffd6ed
+	detect_antivirus.mli \
ffd6ed
 	DOM.mli \
ffd6ed
 	domainxml.mli \
ffd6ed
 	input_disk.mli \
ffd6ed
@@ -73,6 +75,7 @@ SOURCES_ML = \
ffd6ed
 	kvmuid.ml \
ffd6ed
 	OVF.ml \
ffd6ed
 	linux.ml \
ffd6ed
+	detect_antivirus.ml \
ffd6ed
 	modules_list.ml \
ffd6ed
 	input_disk.ml \
ffd6ed
 	input_libvirtxml.ml \
ffd6ed
diff --git a/v2v/convert_windows.ml b/v2v/convert_windows.ml
ffd6ed
index 36cf8c1..3982700 100644
ffd6ed
--- a/v2v/convert_windows.ml
ffd6ed
+++ b/v2v/convert_windows.ml
ffd6ed
@@ -23,6 +23,7 @@ open Common_utils
ffd6ed
 
ffd6ed
 open Regedit
ffd6ed
 
ffd6ed
+open Detect_antivirus
ffd6ed
 open Utils
ffd6ed
 open Types
ffd6ed
 
ffd6ed
@@ -41,14 +42,6 @@ module G = Guestfs
ffd6ed
 
ffd6ed
 type ('a, 'b) maybe = Either of 'a | Or of 'b
ffd6ed
 
ffd6ed
-(* Antivirus regexps that match on inspect.i_apps.app2_name fields. *)
ffd6ed
-let av_rex =
ffd6ed
-  let alternatives = [
ffd6ed
-    "virus"; (* generic *)
ffd6ed
-    "Kaspersky"; "McAfee"; "Norton"; "Sophos";
ffd6ed
-  ] in
ffd6ed
-  Str.regexp_case_fold (String.concat "\\|" alternatives)
ffd6ed
-
ffd6ed
 let convert ~verbose ~keep_serial_console (g : G.guestfs) inspect source =
ffd6ed
   (* Get the data directory. *)
ffd6ed
   let virt_tools_data_dir =
ffd6ed
@@ -145,12 +138,7 @@ let convert ~verbose ~keep_serial_console (g : G.guestfs) inspect source =
ffd6ed
     with_hive "software" ~write:false check_group_policy in
ffd6ed
 
ffd6ed
   (* Warn if Windows guest has AV installed. *)
ffd6ed
-  let has_antivirus =
ffd6ed
-    let check_app { G.app2_name = name } =
ffd6ed
-      try ignore (Str.search_forward av_rex name 0); true
ffd6ed
-      with Not_found -> false
ffd6ed
-    in
ffd6ed
-    List.exists check_app inspect.i_apps in
ffd6ed
+  let has_antivirus = detect_antivirus inspect in
ffd6ed
 
ffd6ed
   (* Open the software hive (readonly) and find the Xen PV uninstaller,
ffd6ed
    * if it exists.
ffd6ed
diff --git a/v2v/detect_antivirus.ml b/v2v/detect_antivirus.ml
ffd6ed
new file mode 100644
ffd6ed
index 0000000..747b225
ffd6ed
--- /dev/null
ffd6ed
+++ b/v2v/detect_antivirus.ml
ffd6ed
@@ -0,0 +1,39 @@
ffd6ed
+(* virt-v2v
ffd6ed
+ * Copyright (C) 2015 Red Hat Inc.
ffd6ed
+ *
ffd6ed
+ * This program is free software; you can redistribute it and/or modify
ffd6ed
+ * it under the terms of the GNU General Public License as published by
ffd6ed
+ * the Free Software Foundation; either version 2 of the License, or
ffd6ed
+ * (at your option) any later version.
ffd6ed
+ *
ffd6ed
+ * This program is distributed in the hope that it will be useful,
ffd6ed
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
ffd6ed
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ffd6ed
+ * GNU General Public License for more details.
ffd6ed
+ *
ffd6ed
+ * You should have received a copy of the GNU General Public License along
ffd6ed
+ * with this program; if not, write to the Free Software Foundation, Inc.,
ffd6ed
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ffd6ed
+ *)
ffd6ed
+
ffd6ed
+(* Detect anti-virus (AV) software installed in Windows guests. *)
ffd6ed
+
ffd6ed
+let rex_virus     = Str.regexp_case_fold "virus" (* generic *)
ffd6ed
+let rex_kaspersky = Str.regexp_case_fold "kaspersky"
ffd6ed
+let rex_mcafee    = Str.regexp_case_fold "mcafee"
ffd6ed
+let rex_norton    = Str.regexp_case_fold "norton"
ffd6ed
+let rex_sophos    = Str.regexp_case_fold "sophos"
ffd6ed
+
ffd6ed
+let rec detect_antivirus { Types.i_type = t; i_apps = apps } =
ffd6ed
+  assert (t = "windows");
ffd6ed
+  List.exists check_app apps
ffd6ed
+
ffd6ed
+and check_app { Guestfs.app2_name = name } =
ffd6ed
+  name      =~ rex_virus     ||
ffd6ed
+  name      =~ rex_kaspersky ||
ffd6ed
+  name      =~ rex_mcafee    ||
ffd6ed
+  name      =~ rex_norton    ||
ffd6ed
+  name      =~ rex_sophos
ffd6ed
+
ffd6ed
+and (=~) str rex =
ffd6ed
+  try ignore (Str.search_forward rex str 0); true with Not_found -> false
ffd6ed
diff --git a/v2v/detect_antivirus.mli b/v2v/detect_antivirus.mli
ffd6ed
new file mode 100644
ffd6ed
index 0000000..9cd5997
ffd6ed
--- /dev/null
ffd6ed
+++ b/v2v/detect_antivirus.mli
ffd6ed
@@ -0,0 +1,23 @@
ffd6ed
+(* virt-v2v
ffd6ed
+ * Copyright (C) 2015 Red Hat Inc.
ffd6ed
+ *
ffd6ed
+ * This program is free software; you can redistribute it and/or modify
ffd6ed
+ * it under the terms of the GNU General Public License as published by
ffd6ed
+ * the Free Software Foundation; either version 2 of the License, or
ffd6ed
+ * (at your option) any later version.
ffd6ed
+ *
ffd6ed
+ * This program is distributed in the hope that it will be useful,
ffd6ed
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
ffd6ed
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ffd6ed
+ * GNU General Public License for more details.
ffd6ed
+ *
ffd6ed
+ * You should have received a copy of the GNU General Public License along
ffd6ed
+ * with this program; if not, write to the Free Software Foundation, Inc.,
ffd6ed
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ffd6ed
+ *)
ffd6ed
+
ffd6ed
+(** Detect anti-virus (AV) software installed in Windows guests. *)
ffd6ed
+
ffd6ed
+val detect_antivirus : Types.inspect -> bool
ffd6ed
+(** Return [true] if anti-virus (AV) software was detected in
ffd6ed
+    this Windows guest. *)
ffd6ed
-- 
ffd6ed
1.8.3.1
ffd6ed