[Libguestfs] [PATCH RFC] sysprep: remove user accounts

Wanlong Gao gaowanlong at cn.fujitsu.com
Wed Apr 18 09:14:39 UTC 2012


Hi Rich,
This patch attends to remove the user accounts in the guest,
I send this out to request your comments, if the concept
is correct and you can point out some syntax error for me.

Thanks a lot,
Wanlong Gao


Remove user accounts except the root user.

Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com>
---
 sysprep/Makefile.am                       |    2 +
 sysprep/sysprep_operation_user_account.ml |   82 +++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+)
 create mode 100644 sysprep/sysprep_operation_user_account.ml

diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am
index f51fc07..9b06804 100644
--- a/sysprep/Makefile.am
+++ b/sysprep/Makefile.am
@@ -48,6 +48,7 @@ SOURCES = \
 	sysprep_operation_ssh_hostkeys.ml \
 	sysprep_operation_ssh_userdir.ml \
 	sysprep_operation_udev_persistent_net.ml \
+	sysprep_operation_user_account.ml \
 	sysprep_operation_utmp.ml \
 	sysprep_operation_yum_uuid.ml \
 	utils.ml
@@ -73,6 +74,7 @@ OBJECTS = \
 	sysprep_operation_ssh_hostkeys.cmx \
 	sysprep_operation_ssh_userdir.cmx \
 	sysprep_operation_udev_persistent_net.cmx \
+	sysprep_operation_user_account.ml \
 	sysprep_operation_utmp.cmx \
 	sysprep_operation_yum_uuid.cmx \
 	main.cmx
diff --git a/sysprep/sysprep_operation_user_account.ml b/sysprep/sysprep_operation_user_account.ml
new file mode 100644
index 0000000..ac07c6d
--- /dev/null
+++ b/sysprep/sysprep_operation_user_account.ml
@@ -0,0 +1,82 @@
+(* virt-sysprep
+ * Copyright (C) 2012 FUJITSU LIMITED
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+open Sysprep_operation
+
+module G = Guestfs
+
+let user_account_perform g root =
+  let typ = g#inspect_get_type root in
+  if typ <> "windows" then (
+    let login_def = "/etc/login.defs" in
+    let lines = Array.to_list (g#read_lines login_def) in
+    let line_min = Array.filter (
+      fun line -> (string_prefix line "UID_MIN")
+    ) lines in
+    let _,min_uid = sscanf line_min "%s %d" (fun a b -> a,b) in
+    let line_max = Array.filter (
+      fun line -> (string_prefix line "UID_MAX")
+    ) lines in
+    let _,max_uid = sscanf line_max "%s %d" (fun a b -> a,b) in
+    let passwd = "/etc/passwd" in
+    let shadow = "/etc/shadow" in
+    let group = "/etc/group" in
+    let lines = Array.to_list (g#read_lines passwd) in
+    let lines_passwd = Array.to_list (g#read_lines passwd) in
+    let lines_shadow = Array.to_list (g#read_lines shadow) in
+    let lines_group = Array.to_list (g#read_lines group) in
+    List.iter (
+      fun line ->
+        let user_line = string_split ":" line in
+        let user_line = Array.of_list user_line in
+        if user_line.(2) >= min_uid and user_line.(2) <= max_uid then (
+          let user = user_line(0) in
+          let lines_passwd = List.filter (
+            fun line -> not (string_prefix line user)
+          ) lines_passwd in
+          let lines_shadow = List.filter (
+            fun line -> not (string_prefix line user)
+          ) lines_shadow in
+          let lines_group = List.filter (
+            fun line -> not (string_prefix line user)
+          ) lines_group in
+          let dir = String.concat "/home/" user in
+          g#rm_rf dir
+        )
+    ) lines in
+    let file = String.concat "\n" lines_passwd ^ "\n" in
+    g#write passwd file in
+    let file = String.concat "\n" lines_shadow ^ "\n" in
+    g#write shadow file in
+    let file = String.concat "\n" lines_group ^ "\n" in
+    g#write group file
+  )
+  else []
+
+let user_account_op = {
+  name = "user-account";
+  pod_description = "\
+Remove the user accounts except \"root\" in the guest.
+
+Remove the user accounts and their home directory except
+the \"root\" account.";
+  extra_args = [];
+  perform = user_account_perform;
+}
+
+let () = register_operation bash_history_op
-- 
1.7.10




More information about the Libguestfs mailing list