Fix pep8 error
[snf-image-creator] / image_creator / os_type / unix.py
index 20b971c..655fd49 100644 (file)
@@ -34,7 +34,8 @@
 import re
 import sys
 
-from image_creator.os_type import OSBase
+from image_creator.os_type import OSBase, exclude_task
+from image_creator.util import warn, output
 
 
 class Unix(OSBase):
@@ -63,41 +64,116 @@ class Unix(OSBase):
 
             user, passwd = match.groups()
             if len(passwd) > 0 and passwd[0] == '!':
-                print "Warning: Ignoring locked %s account." % user
+                warn("Ignoring locked %s account." % user)
             else:
                 users.append(user)
 
         return users
 
-    def data_cleanup(self):
-        self.data_cleanup_userdata()
-        self.data_cleanup_tmp()
-        self.data_cleanup_log()
-        self.data_cleanup_mail()
-        self.data_cleanup_cache()
+    @exclude_task
+    def data_cleanup_user_accounts(self, print_header=True):
+        """Remove all user account with id more than 1000"""
 
-    def data_cleanup_cache(self):
+        if print_header:
+            output('Removing all user accounts with id greater than 1000')
+
+        # Remove users from /etc/passwd
+        passwd = []
+        removed_users = {}
+        for line in self.g.cat('/etc/passwd').splitlines():
+            fields = line.split(':')
+            if int(fields[2]) > 1000:
+                removed_users[fields[0]] = fields
+            else:
+                passwd.append(':'.join(fields))
+
+        self.g.write('/etc/passwd', '\n'.join(passwd) + '\n')
+
+        # Remove the corresponding /etc/shadow entries
+        shadow = []
+        for line in self.g.cat('/etc/shadow').splitlines():
+            fields = line.split(':')
+            if fields[0] not in removed_users:
+                shadow.append(':'.join(fields))
+
+        self.g.write('/etc/shadow', "\n".join(shadow) + '\n')
+
+        # Remove the corresponding /etc/group entries
+        group = []
+        for line in self.g.cat('/etc/group').splitlines():
+            fields = line.split(':')
+            # Remove groups tha have the same name as the removed users
+            if fields[0] not in removed_users:
+                group.append(':'.join(fields))
+
+        self.g.write('/etc/group', '\n'.join(group) + '\n')
+
+        # Remove home directories
+        for home in [field[5] for field in removed_users.values()]:
+            if self.g.is_dir(home) and home.startswith('/home/'):
+                self.g.rm_rf(home)
+
+    def data_cleanup_passwords(self, print_header=True):
+        """Remove all passwords and lock all user accounts"""
+
+        if print_header:
+            output('Cleaning up passwords & locking all user accounts')
+
+        shadow = []
+
+        for line in self.g.cat('/etc/shadow').splitlines():
+            fields = line.split(':')
+            if fields[1] not in ('*', '!'):
+                fields[1] = '!'
+
+            shadow.append(":".join(fields))
+
+        self.g.write('/etc/shadow', "\n".join(shadow) + '\n')
+
+    def data_cleanup_cache(self, print_header=True):
         """Remove all regular files under /var/cache"""
+
+        if print_header:
+            output('Removing files under /var/cache')
+
         self.foreach_file('/var/cache', self.g.rm, ftype='r')
 
-    def data_cleanup_tmp(self):
+    def data_cleanup_tmp(self, print_header=True):
         """Remove all files under /tmp and /var/tmp"""
+
+        if print_header:
+            output('Removing files under /tmp and /var/tmp')
+
         self.foreach_file('/tmp', self.g.rm_rf, maxdepth=1)
         self.foreach_file('/var/tmp', self.g.rm_rf, maxdepth=1)
 
-    def data_cleanup_log(self):
+    def data_cleanup_log(self, print_header=True):
         """Empty all files under /var/log"""
+
+        if print_header:
+            output('Emptying all files under /var/log')
+
         self.foreach_file('/var/log', self.g.truncate, ftype='r')
 
-    def data_cleanup_mail(self):
+    @exclude_task
+    def data_cleanup_mail(self, print_header=True):
         """Remove all files under /var/mail and /var/spool/mail"""
+
+        if print_header:
+            output('Removing files under /var/mail and /var/spool/mail')
+
         self.foreach_file('/var/spool/mail', self.g.rm_rf, maxdepth=1)
         self.foreach_file('/var/mail', self.g.rm_rf, maxdepth=1)
 
-    def data_cleanup_userdata(self):
+    def data_cleanup_userdata(self, print_header=True):
         """Delete sensitive userdata"""
+
         homedirs = ['/root'] + self.ls('/home/')
 
+        if print_header:
+            output('Removing sensitive user data under %s' % " ".
+                                                        join(homedirs))
+
         for homedir in homedirs:
             for data in self.sensitive_userdata:
                 fname = "%s/%s" % (homedir, data)